Modern async iteration in JavaScript with Array.fromAsync()
Front-end work involves a lot of asynchronous data: API responses, streams, lazy-loaded content. JavaScript has long had tools for async logic, but working with async iterables has often meant verbose, manual handling.
Array.fromAsync() is a newer addition that turns async data sources into arrays, and it works with async/await the way you’d expect.
What’s Array.fromAsync()?
Array.fromAsync() is a new (ES2024) method added to JavaScript that allows you to convert…
- Async iterables
- Regular (sync) iterables
…into arrays. It’s similar to Array.from(), but built to handle asynchronous data sources:
Array.fromAsync(source[, mapFn[, thisArg]]) // Returns a Promise<Array>
source: An async iterable or a sync iterable.mapFn(optional): A mapping function applied to each item (can be async).thisArg(optional): Value to use asthisinsidemapFn.
Array.fromAsync() returns a Promise that resolves to the resulting array.
Why and when to use it
If you’ve ever had to collect items from an async generator, page through an API, or turn a stream into an array, this is the method for that.
Common use cases:
- Transforming data from async APIs or generators
- Flattening asynchronous streams of events or responses
- Replacing verbose
for await...ofloops with a single line - Simplifying Promise arrays with optional async mapping
You can…
Convert an async generator to an array
async function* generateNumbers() {
yield 1;
yield 2;
yield 3;
}
const result = await Array.fromAsync(generateNumbers());
console.log(result); // [1, 2, 3]
This converts values yielded from an async generator into an array in a single line. It’s a concise alternative to manually iterating with for await...of.
Apply a mapping function
const result = await Array.fromAsync(generateNumbers(), x => x * 2);
console.log(result); // [2, 4, 6]
The second argument lets you transform each item as it’s added to the array. Here, each number is doubled synchronously.
Use an async mapping function
const result = await Array.fromAsync(generateNumbers(), async x => {
return x * 10;
});
console.log(result); // [10, 20, 30]
You can also use an async function as the mapper, enabling asynchronous operations like fetching or waiting during transformation.
Compared to other patterns
Before Array.fromAsync(), you might have written:
async function collect(generator) {
const result = [];
for await (const value of generator) {
result.push(value);
}
return result;
}
Or used a Promise-based approach:
const promises = [fetch(url1), fetch(url2)];
const results = await Promise.all(promises);
These patterns still work and are sometimes preferable. But Array.fromAsync() is shorter, especially with iterators or mapping functions.
Error handling
Errors during iteration or mapping are automatically propagated, whether sync or async. Use try...catch to handle them:
try {
const result = await Array.fromAsync(faultyGenerator(), async x => {
if (x === 'error') throw new Error('Something went wrong!');
return x;
});
} catch (err) {
console.error('Caught an error:', err.message);
}
In practice
Loading paginated data
async function* fetchPaginatedData() {
let page = 1;
while (page <= 3) {
const response = await fetch(`/api/data?page=${page}`);
const data = await response.json();
for (const item of data.items) {
yield item;
}
page++;
}
}
const allItems = await Array.fromAsync(fetchPaginatedData());
Array.fromAsync() collects all the items across pages into a single array, replacing manual loops and accumulation logic.
Reading from a Stream (Web Streams API)
async function* streamToAsyncIterator(stream) {
const reader = stream.getReader();
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
yield value;
}
} finally {
reader.releaseLock();
}
}
const response = await fetch('/large-file.txt');
const chunks = await Array.fromAsync(streamToAsyncIterator(response.body));
// Optional: Decode chunks into text if the stream is textual (e.g., a .txt or JSON file)
const decoder = new TextDecoder();
const textChunks = chunks.map(chunk => decoder.decode(chunk));
Array.fromAsync() is especially useful when working with streamed file downloads, real-time data, or chunked responses.
Browser support & polyfills
Array.fromAsync() is supported in all modern browsers (Chrome 121+, Firefox 115+, Safari 16.4+, Edge 121+) and in Node.js 22+.
Polyfill option (simple version)
async function arrayFromAsync(source, mapFn, thisArg) {
const result = [];
let i = 0;
mapFn = typeof mapFn === 'function' ? mapFn : x => x;
for await (const item of source) {
result.push(await mapFn.call(thisArg, item, i++));
}
return result;
}
Quick comparison
| Feature | Array.from() |
Array.fromAsync() |
|---|---|---|
| Handles async iterables | ❌ | ✅ |
| Returns a Promise | ❌ | ✅ |
| Supports async mapping fn | ❌ | ✅ |
Can replace for await...of |
❌ | ✅ |
When I’d use it
I’d use Array.fromAsync() whenever I’m collecting async generator output or a stream into an array, instead of writing a for await...of loop by hand.