Used for iterating over async iterable objects with await
inside the loop body.
async function processArray(array) {
for await (let value of array) {
console.log(value);
}
}
const asyncIterable = {
[Symbol.asyncIterator]() {
return {
i: 0,
async next() {
if (this.i < 3) {
return Promise.resolve({ value: this.i++, done: false });
} else {
return Promise.resolve({ done: true });
}
}
};
}
};
processArray(asyncIterable);