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.