Code Monkey home page Code Monkey logo

readable-streams's Introduction

readable-streams

Playing with some readable streams and async iterators examples and creating this for tomorrow when I have probably forgotten everything already.

Reading files

Read with on data strategy

Not great for buffering, there's no flow control. Data is read as quickly as possible.

function readFile(filename) {
  const stream = createReadStream(filename);

  stream.on('data', (chunk) => {
    console.log(chunk);
  });
}

Read with on readable strategy

By using the readable event, the consumer has the freedom of deciding whether to read or not from the stream when the event is triggered.

function readFilePolling(filename) {
  const stream = createReadStream(filename);

  stream.on('readable', () => {
    let data;
    while ((data = stream.read()) !== null) {
      console.log(data);
    }
  });
}

Read with async iterators strategy

A very elegant option to using the readable event is to use async iterators. The consumer still has control on how data is consumed and additionally it brings more readability.

async function readFileAsyncIterator(filename) {
  let chunk;
  for await (chunk of createReadStream(filename)) {
    console.log(chunk);
  }
}

Reading from generators

Now, instead of reading information from files, let's read it from generators. This time let's just focus on the async iterators strategy.

Generator example:

function* generate() {
  for (let i = 0; i <= 30; i++) {
    yield i;
  }
}

Read with async iterator:

async function readStreamFromAsyncIterator() {
  const stream = Readable.from(generate());
  let chunk;
  for await (chunk of stream) {
    console.log(chunk);
  }
}

Note that Readable.from gets an iterable as parameter. We could pass an array for example.

Also note that it's not convenient to use this approach just to iterate over the values generated by a generator, as generate() is iterable so doing something like this would be more appropriate:

for (chunk of generate()) {
  console.log(chunk);
}

Reading from async generators

Combining async generators and async iterators is a quite clean option when the generator needs to be async (not the case of the example, though).

Async generator example:

async function* asyncGenerate() {
  for (let i = 0; i <= 30; i++) {
    yield i;
  }
}

Read with async iterator:

async function readAsyncGenerator() {
  for await (const chunk of asyncGenerate()) {
    console.log(chunk);
  }
}

Resources

readable-streams's People

Contributors

notsag-dev avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.