Asynchronous Control Flow Patterns with Streams

Learn how to execute asynchronous sequential iteration using only streams.

We'll cover the following

Going through the examples that we’ve presented so far, it should be clear that streams can be useful not only to handle I/O but also as an elegant programming pattern that can be used to process any kind of data. But the advantages don’t end at its simple appearance; streams can also be leveraged to turn “asynchronous control flow” into “flow control,” as we’ll see in this section.

Sequential execution

By default, streams will handle data in sequence. For example, the _transform() function of a Transform stream will never be invoked with the next chunk of data until the previous invocation completes by calling callback(). This is an important property of streams, crucial for processing each chunk in the right order, but it can also be used to turn streams into an elegant alternative to the traditional control flow patterns.

Some code is always better than too much explanation, so let’s work on an example to demonstrate how we can use streams to execute asynchronous tasks in sequence. Let’s create a function that concatenates a set of files received as input, making sure to honor the order in which they are provided. Let’s create a new module called concat-files.js and define its contents as follows:

Get hands-on with 1200+ tech skills courses.