GithubHelp home page GithubHelp logo

frully / stream-worker Goto Github PK

View Code? Open in Web Editor NEW

This project forked from goodeggs/stream-worker

0.0 2.0 0.0 33 KB

Execute an async function per stream data event, pausing the stream when a concurrency limit is saturated

CoffeeScript 79.78% JavaScript 20.22%

stream-worker's Introduction

stream-worker build status

Execute an async function per stream data event, pausing the stream when a concurrency limit is saturated. Inspired by async.queue, optimized for streams.

Supports promises and callbacks.

The Basics

npm install stream-worker

Requiring:

var streamWorker = require('stream-worker');

Promise style:

function doWork(data){
  /* ... do some work with data ... */
  return Promise.resolve();
}
streamWorker(stream, doWork, {promises : true, concurrency : 10})
.then(function() {
  /* ... the stream is exhausted and all workers are finished ... */
}, function(err) {
  /* ... there was an error processing the stream ... */
})

Callback style:

function doWork(data, done){
  /* ... do some work with data ... */
  return done(err);;
}
streamWorker(stream, doWork, {promises : false, concurrency :10},
  function(err) {
    /* ... the stream is exhauseted and all workers are finished ... */
  }
);

Signature

streamWorker(stream, work, options, done)

Where options is an object with 2 optional parameters:

Parameter Default Description
promises false true if you want to use the above promises style
concurrency 10 specifies how many concurrent workers you want doing work in the stream

And done is a callback function if you use the callback workflow.

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.