Sharing Top Content from the Angular-sphere.

Using async iteration natively in Node.js

  • Second, you receive the data by listening to the events and : – – Starting with Node.js v10, you can also use asynchronous iteration to read a file asynchronously.
  • However, this kind of loop is only available within async functions and async generators.
  • With async generators, you can go one step further: They can be the source of an async iterable.
  • The following function takes an async iterable over strings and returns an async iterable over lines: – – This function takes lines and numbers them: – – The function reads a text file via a readable stream and applies the two async generators to it, therefore numbering the lines in…
  • Based on async iteration, we can now have combinators such as and for asynchronous data.

Node.js v10 will be released on April 30, 2018. In that version, there will be experimental support for asynchronously iterating over readable streams. This blog post explains how that works.

Node.js v10 will be released on April 30, 2018. In that version, there will be experimental support for asynchronously iterating over readable streams. This blog post explains how that works.

In this section, we examine two ways of reading data from a stream asynchronously: via callbacks and via asynchronous iteration.

To read the contents of a file asynchronously, you can use callbacks, as follows.

Second, you receive the data by listening to the events and :

Starting with Node.js v10, you can also use asynchronous iteration to read a file asynchronously. Readable streams have a property whose key is , which enables the loop to iterate over their chunks. However, this kind of loop is only available within async functions and async generators. That’s why we have to use an async function:

So far, we have seen how you can use async functions as sinks of async iterables. With async generators, you can go one step further: They can be the source of an async iterable. Or they can transform an async iterable (as both sink and source). The latter works as follows. The async generator:

That is, if you chain async generators, you can process input similarly to Unix piping. Let’s look at a pipe with two async generators.

The following function takes an…

Using async iteration natively in Node.js