All about Javascript Streams Api

Introduction

Streams are a powerful feature of the Javascript language. They provide a common interface for async operations, allowing us to do things like read data from files or network connections without having to worry about how long each operation will take. In this post we’ll explain what streams are and how they work, then show some examples of using them with popular libraries like RxJS and NodeJS.

What is a stream?

A stream is a sequence of data. It can either be infinite or finite, and it can be a single value or an array. Streams are created using the new operator, so they’re technically objects (and not functions).

A stream represents an asynchronous operation that returns data over time–for example, reading from disk or network connection.

Streams provide an interface for dealing with potentially infinite sequences of values such as number sequences (0..1), character codes (65..90) and dates (today).

Creating a stream

To create a stream, you can use the new keyword and pass in a function to the constructor. This is how you do it:

  • “new Stream(callback)”
  • Where callback is any valid Javascript function.

You can also create streams from arrays, generator functions, and other streams by passing them into this constructor as well:

var stream = new Stream(callback);
var arrayStream = new Stream(array);
var generatorStream = new Stream({
  next: function() {
    return this.value;
  }
});

The Stream API is a nice addition to Javascript, but it’s not yet clear how widely it will be adopted. It does seem like a good way to handle async operations, and it may be useful for things like working with WebSockets or other types of socket connections that need access to multiple events happening at the same time.

Async iteration with for await…of

The for await…of loop is an asynchronous iterator that returns a stream of data. It’s similar to the regular for…in loop, except that it can be used with Promises and other async iterables.

For example:

const fruits = ['apples', 'oranges']

for await (fruit of fruits) {
  console.log(fruit)
}
//Output: applesoranges

Using the pipe to call a function with each item of the stream

The pipe method is used to call a function with each item of the stream. The pipe method takes in 3 arguments: the first one is the stream, the second one is the function that will be called with each item of the stream and finally an optional third argument which allows you to pass any arguments needed by your custom function.

Here’s an example:

const numbers = [1, 2];
numbers.pipe(myCustomFunction).forEach(console.log);

Streams are a useful tool for async programming

Streams are a way of processing data asynchronously. They can be used for both reading and writing, and they’re very useful for building web apps that need to work with large amounts of data in real-time.

Streams have been around since ES6 (ECMAScript 2015), but they haven’t seen much adoption outside of NodeJS until recently. Some people believe this is because the Stream API can be difficult to understand at first glance–but once you understand it, streams will become an indispensable tool in your programming arsenal!

Conclusion

Javascript streams are a powerful tool for async programming. They allow you to work with data as it comes in, without having to wait for it all before doing anything with it. The pipe function allows us to use streams as if they were normal arrays or objects, which makes our code easier to read and write.

Leave a Reply