Need every page from an async generator in one array? Array.fromAsync
Array.fromAsync() builds an array from an async iterable and returns a Promise. Per MDN, it awaits each value one after another, while Promise.all awaits them all at once, so choose by the job.

You have an API that returns results page by page, and you wrote an async generator that fetches the pages one after another. But when you want all the results in one array, you're back to writing a for await loop with push. In this article you will see how one line does the same job, and when it isn't the right tool.
What is Array.fromAsync()?
Array.fromAsync() is a JavaScript method that builds a new array from an async iterable or a regular iterable, and returns a Promise whose value is that array.
According to the MDN page on Array.fromAsync(), it works like Array.from() but for things that arrive over time, and the page marks it Baseline Widely available, working across browsers since January 2024.
async function* pages() {
let url = "/api/items?page=1";
while (url) {
const res = await fetch(url);
const body = await res.json();
yield* body.items;
url = body.next;
}
}
const items = await Array.fromAsync(pages());Why aren't Array.from() or Promise.all() enough?
Array.from() doesn't handle an async iterable, and Promise.all() takes all the values up front. An async generator doesn't give you that: it gives you one value, and only when that's done does it give you the next.
The common mistake is treating the two as the same thing. MDN explains the difference: Array.fromAsync() awaits each value one after another, while Promise.all() awaits them all at once. Also, Array.fromAsync() doesn't ask for the next value until the current one is settled, while Promise.all() takes them all in advance.
|
| |
|---|---|---|
Awaits the values | One after another | All at once |
Takes the next value | After the current one settles | All in advance |
When do you use each one?
You use Array.fromAsync() when the values must arrive in order or one at a time, and Promise.all() when the requests are independent and you want them in parallel.
API pages in sequence, where each page gives you the link to the next:
Array.fromAsync(), because you can't know the next page before the current one arrives.A service with a request limit that you don't want to hit with twenty requests at once:
Array.fromAsync(), because it asks one at a time.Ten independent requests and you want the fastest result:
Promise.all(). MDN's own example shows the sequential approach is slower in that case.You want to change each value while collecting: pass
mapFnas the second argument, and its output is awaited too.
If you're coming from Promise.try and Promise.withResolvers, this is a third piece of the same family. And with iterator helpers you can filter before you collect.
Where do you start now?
The lesson for us as developers: "in order" and "in parallel" are two different decisions, and the code should say which one you mean. Look in your code today for a for await loop whose only job is to push into an array, and replace it with Array.fromAsync().
And if the requests can hang, add a timeout with AbortSignal to each one. Where in your code are you collecting async results by hand?
FAQ
Does it work in every browser?
MDN marks it Baseline Widely available, meaning it has worked across browsers since January 2024. If you support older browsers than that, check the table on the page.
What happens if one of the promises rejects?
In MDN's example, the await on Array.fromAsync sits inside try and catch, and the error lands in catch. With a sync iterable, MDN says it calls the iterator's return() so it can clean up.
Can I map while collecting?
Yes. The second argument is mapFn, and MDN says its output is awaited too, so it can return a promise.


