srough

Like `through` for async streams.

Usage no npm install needed!

<script type="module">
  import srough from 'https://cdn.skypack.dev/srough';
</script>

README

srough

Like through for async streams.

In 90% of the cases you should use through. Use srough only when you're stream is async.

The main idea is that streams are sync units that can perform async operations.

Data objects should be processed and written downstream in the same order they arrived from upstream.

Let's say we have a Transform stream that receives URL's from upstream and writes the response headers downstream.

Now, let's suppose that the urls are being written in the following order:

  1. slowserver.com
  2. fastserver.com

Downstream, should receive the response headers in the same order exactly. If we'll use through the URLs would be processed concurrently and the response would be written downstream in a random order.

Eventually, srough is exactly the same thing as through, just with sync callback.

Usage

var srough = require("srough");

var stream = srough(function write(data, done) {
                 somethingAsync(data, function (err, res) {
                    this.queue(res) //data *must* not be null
                    done(); // After you call this callback, will process next...
                 });
               },
               function end (done) { //optional
                 this.queue(null);
                 done();
               });

Basically same API as through just with a synchronization callback.