Skip to the solution
MediumFrontend5 min read

What is Array.fromAsync() and how does it handle async iterables differently from Array.from()?

341 views

The problem

Question presented to candidate: "If you call Array.from() on a plain array that happens to contain some Promise objects mixed with regular values, does it wait for those Promises to resolve before returning? What about Array.fromAsync()?"

What a strong answer should cover:

  • πŸ“Œ Interview term: Array.fromAsync(source, mapFn?) β€” the async counterpart to Array.from, returning a real Promise that resolves to a real array collected from an async iterable (or a regular iterable/array-like).
  • πŸ“Œ Interview term: the real, direct answer to the prompt β€” verified directly: plain Array.from() on an array containing Promise objects genuinely does NOT await them β€” the resulting array genuinely still contains the raw, un-resolved Promise objects themselves. Array.fromAsync() on the SAME input genuinely DOES await each Promise element, correctly collecting their resolved values instead.
  • πŸ“Œ Interview term: collecting from a genuine async generator β€” verified directly: Array.fromAsync correctly collected every value from a real async generator, including one that performed a genuine await in the middle of its own sequence β€” something plain Array.from cannot do at all, since it has no way to await anything.
  • πŸ“Œ Interview term: the optional mapping function β€” verified directly: Array.fromAsync accepts the identical second mapFn argument Array.from does, applied to each resolved value.
  • A precise answer names the real, practical use case: converting an async generator (or a stream of Promises) into a concrete, materialized array when the FULL, complete result set is genuinely needed before proceeding, rather than processing values one at a time as they arrive via for await...of (covered in this bank's own dedicated async-iterators question).

Clarifying questions expected:

  • None β€” this is a definitional/comparison question; directly answering the prompt's own Promise-array scenario (does-it-await-or-not) is the strong signal.

Code / implementation expected: Yes β€” the direct side-by-side Array.from vs. Array.fromAsync on the identical Promise-containing array is the clearest, most convincing demonstration.

  • es2024
  • array
  • async-iterable

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

5 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal proof: Array.from genuinely does not await Promise elements (keeps raw Promises), while Array.fromAsync genuinely does, plus collecting from a real async generator

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 99 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

What does `Promise.try()` do and why is it better than `Promise.resolve().then(fn)`?

Continue