Have been trying to figure out what exactly for await .. of does. However, even reading the specs I haven't been able to find exactly what it does.
Here is my guess:
const it = iterable[Synbol.asyncIterator]();
while (true) {
const { done, value } = await it.next();
if (done) return;
// User code...
}
However, if the iterator implements return and throw I'm not sure where/how they come into play?
One guess is:
const it = iterable[Synbol.asyncIterator]();
try {
while (true) {
const { done, value } = await it.next();
if (done) return;
try {
// User code...
} catch (err) {
const { done } = await it.throw(err);
if (done) return;
}
}
} finally {
it.return();
}
The
throwmethod of the iterator is not used at all in the iteration protocol.The
returnmethod of the iterator is called if the user code evaluates to an abrupt completion that prematurely ends the iteration (i.e. before(await it.next()).doneistrue):return,breakorcontinue outer_labelstatementThe steps of the AsyncIteratorClose procedure are really hard to accurately represent as JS code, so I'll avoid presenting an attempt at desugaring.
The
for await … ofsteps are exactly those of thefor … ofloop, except for accessing the iterator bySymbol.asyncIteratorinstead ofSymbol.iterator, andawaiting the return values of.next()and.return()invocations.