JS Built-ins
← Array19 / 226
ArrayArray.fromAsyncadvanced

Array.fromAsync() β€” map each element

πŸ“– Usage Example
const nums = [1, 2, 3]
const doubled = await Array.fromAsync(nums, async n => n * 2)
// β†’ [2, 4, 6]

Pass an async mapping function to Array.fromAsync() to transform each element as it resolves.

Array.fromAsync() β€” with a mapping function

Like Array.from(), Array.fromAsync() accepts an optional mapping function as its second argument. The mapper can itself be async (returning a promise).

This exercise simulates the pattern synchronously.

Challenge: Implement doubleAll that doubles every number in an array using Array.from with a mapping function.

doubleAll([1, 2, 3]) // β†’ [2, 4, 6]
πŸ’‘ Hints (2)
  • β€’ `Array.from(iterable, mapFn)` applies `mapFn` to each element during construction.
  • β€’ The async version `Array.fromAsync(iterable, async x => ...)` awaits each mapped value.