Array.fromAsync: The One-Liner to Collect Async Iterable Results into an Array
JavaScript developers often use verbose for-await loops to collect values from async iterables like streams, generators, or database cursors into a plain array. The built-in Array.fromAsync method achieves the same result in a single awaited call, eliminating that boilerplate. Like Array.from, it accepts an optional mapping function, and will also await any promise returned by that mapper before proceeding. Unlike Promise.all, Array.fromAsync processes values sequentially — awaiting each item before pulling the next — making it well-suited for lazy sources such as paginated API generators. For concurrent fetching over a known array, Promise.all remains the appropriate choice.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in