Question presented to candidate: "Does for await...of only work on async generators, or can it also be used on a plain array containing Promises? What about a custom object you write yourself — what does it need to implement?"
What a strong answer should cover:
- 📌 Interview term: the async iterable protocol — an object implementing
[Symbol.asyncIterator](), returning an async iterator whosenext()genuinely returns a Promise of{ value, done }, rather than the plain synchronous object the regular iterator protocol (covered in this bank's own dedicated question) returns. - 📌 Interview term:
for await...of— consumes an async iterable, genuinelyawaiting each value in turn before running the loop body — verified directly with a real async generator involving a genuine mid-sequence delay. - 📌 Interview term: the real, direct answer to the prompt's first question — verified directly:
for await...ofgenuinely works on a PLAIN array of Promises too, not just a true async generator/iterable — arrays are already regular (synchronous) iterables, andfor await...ofgenuinelyawaits each yielded value regardless of whether the underlying iterable is truly async or merely synchronous-but-yielding-Promises. - 📌 Interview term: the real, direct answer to the prompt's second question — verified directly: a hand-written custom object implementing
[Symbol.asyncIterator](), returning an object with a genuinelyasync next()method, correctly worked withfor await...of— confirming the real, minimal requirement is exactly that one method, following the async mirror of the regular iterable protocol. - A precise answer names that an
async function*(an async generator) is genuinely both an async iterator AND an async iterable simultaneously — the identical dual-protocol relationship this bank's own regular generator-function question verifies for plain generators, just with the async variants.
Clarifying questions expected:
- None — this is a definitional/technical question; directly answering both halves of the prompt (works on plain Promise arrays too; needs exactly
[Symbol.asyncIterator]) is the strong signal.
Code / implementation expected: Yes — real proof across all three cases (async generator, plain Promise array, hand-written custom object) is the clearest, most convincing demonstration.