Async generators and for-await-of loops

ES2018 defines the specifications for async generators and for-await-of loops. These features are already available in most browsers and will be of great help when programming asynchronously in JavaScript. They will mostly simplify the creation of queues and loops when iterating over async requests. Moreover, using async iterators, iterables and generators with async calls will be made very easy with the use of promises. Here is a simple code example using these new features:

async function* readLines(path) {    let file = await fileOpen(path);    try {        while (!file.EOF) {            yield await file.readLine();        }
    } finally {        await file.close();    }}

Get Mastering The Faster Web with PHP, MySQL, and JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.