FrontendDeveloper.in

ECMAScript question detail

Async iterators

ECMAScript 6 provides built-in support for synchronously iterating over data using iterators. Both strings and collections objects such as Set, Map, and Array come with a Symbol.iterator property which makes them iterable.

const arr = ['a', 'b', 'c', 'd'];
const syncIterator = arr[Symbol.iterator]();

console.log(syncIterator.next()); // {value: a, done: false}
console.log(syncIterator.next()); // {value: b, done: false}
console.log(syncIterator.next()); // {value: c, done: false}
console.log(syncIterator.next()); // {value: d, done: false}
console.log(syncIterator.next()); // {value: undefined, done: true}

But these iterators are only suitable for representing synchronous data sources.

In order to access asynchronous data sources, ES2018 introduced the AsyncIterator interface, an asynchronous iteration statement (for-await-of), and async generator functions.

Back to all ECMAScript questions
Get LinkedIn Premium at Rs 399