Have ideas to improve npm?Join in the discussion! »

through2-parallel

0.1.3 • Public • Published

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}, ...).

Install

npm i through2-parallel

DownloadsWeekly Downloads

32

Version

0.1.3

License

MIT

Last publish

Collaborators

  • avatar