JS Built-ins
← Array20 / 226
ArrayArray.fromAsyncadvanced

Array.fromAsync() β€” works with sync iterables

πŸ“– Usage Example
const set = new Set([10, 20, 30])
const arr = await Array.fromAsync(set)
// β†’ [10, 20, 30]

Array.fromAsync() also works with synchronous iterables like Set or Map.

Array.fromAsync() β€” sync iterable input

Array.fromAsync() is not limited to async iterables. It also accepts sync iterables (like plain arrays or Sets), returning a Promise that resolves to the collected elements. This makes it a drop-in for Array.from when you need a Promise regardless.

This exercise demonstrates the pattern synchronously using Array.from on a Set.

Challenge: Implement syncToArray that converts a Set<number> to an array using Array.from.

syncToArray(new Set([1, 2, 3])) // β†’ [1, 2, 3]
πŸ’‘ Hints (2)
  • β€’ `Array.fromAsync` wraps the result in a Promise even for sync inputs.
  • β€’ `Array.from` and `Array.fromAsync` accept any iterable (arrays, Sets, Maps, generators, strings).