for await...of sequential execution limits with async iterators
0 reputation · 12 Jul 2024, 02:28 UTC
Designing a streaming pipeline that consumes an async iterable with for await...of raises questions about execution guarantees and resource cleanup. The documented behavior is sequential pause at each yield point until the promise from next() resolves, with no parallel iteration unless explicitly composed.
A specific concern is the interaction with synchronous iterables and early termination. Using for await...of on a non-async iterable wraps each value in a resolved promise, adding overhead. Early exit via break or exception should trigger the iterator's return() method for cleanup, but the conditions for reliable invocation are unclear.
Is the loop body guaranteed to run only after next() resolves for each iteration? Is return() reliably called on break or thrown error to allow cleanup of the async iterator? What performance characteristics arise when for await...of is applied to a plain synchronous iterable versus a true async iterable?