ASYNCHRONOUS ITERATION
Asynchronous execution and the iterator protocol are two extremely pervasive themes in new ECMAScript features over recent releases. Asynchronous execution involves releasing control of the execution thread to allow slow operations to finish before regaining control, and the iterator protocol involves defining a canonical ordering for arbitrary objects. Asynchronous iteration is merely the logical assimilation of these two concepts.
A synchronous iterator provides you with a { value, done }
pair each time next()
is invoked. Of course, this requires that the computation and resource fetching needed to determine the contents of this pair be completed by the time the next()
invocation exits—otherwise, these values are indeterminate. When using a synchronous iterator to iterate over values that are determined asynchronously, the main execution thread will be blocked while waiting for the asynchronous operation to complete.
With asynchronous iterators, this problem...