Menu

1 / 4

Array.fromAsync Runs Your Async Work One at a Time

JavaScript in Plain English - Medium·Ferid Brković·3 days ago
#Mhhm9tjO
Reading 0:00
15s threshold

Member-only story

Array.fromAsync Runs Your Async Work One at a Time

It looks like Promise.all over map, but the mapper waits for each result. Here is when that helps and when it hurts.

Ferid Brković

7 min read6 hours ago

Press enter or click to view image in full size

Array.fromAsync(ids, loadPrice) reads like the modern way to write Promise.all(ids.map(loadPrice)) . Same inputs, same array of results, in the same order, with less punctuation.

It is not the same call. With a mapper, Array.fromAsync waits for each result before it asks for the next one. Five requests of 50 ms each take about 250 ms instead of about 50, and nothing in the output tells you.

The same result, five times slower

To see what happens you need to count, not just time. The helper below stands in for a network call.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.