through2-parallel

Parallel Transform stream with an interface lifted from through2

Usage no npm install needed!

<script type="module">
  import through2Parallel from 'https://cdn.skypack.dev/through2-parallel';
</script>

README

NPM version Build Status Coverage Status Dependency Status

through2-parallel

Creates a through stream (Transform stream) which executes in parallel while maintaining the order of the emitted chunks.

Stability: Experimental

Usage

var throughParallel = require('through2-parallel');

var stream = throughParallel.obj(function(chunk, enc, cb) {
  var self = this;
  setTimeout(function() {
    console.log("Completed " + chunk.order);
    self.push(chunk.order);
    cb();
  }, chunk.time);
  console.log("Started " + chunk.order);
});

stream.on('data', function(chunk) {
  console.log("Emitted: " + chunk);
});

stream.write({time: 500, order: 1});
stream.write({time: 200, order: 2});
stream.write({time: 100, order: 3});
stream.end();

The code above will print (default concurrency is 2):

Started 1
Started 2
Completed 2
Completed 1
Emitted: 1
Emitted: 2
Started 3
Completed 3
Emitted: 3

API

throughParallel([options], [transform], [flush])

  • options: Options to pass to the Transform stream plus one specific option:
    • concurrency: defaults to 2 and specifies how many tasks can run in parallel
  • transform: the _transform function.
  • flush: the _flush function.

throughParallel.obj([options], [transform], [flush])

A syntactic sugar for throughParallel({objectMode: true}, ...).