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 toArray.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.fromAsynccorrectly collected every value from a real async generator, including one that performed a genuineawaitin the middle of its own sequence — something plainArray.fromcannot do at all, since it has no way to await anything. - 📌 Interview term: the optional mapping function — verified directly:
Array.fromAsyncaccepts the identical secondmapFnargumentArray.fromdoes, 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.