node package manager

funq

funq

Greenkeeper badge NPM version Build status License Code style

A simple asynchronous function queue, executing either sequentially or in parallel.

Installation

$ npm install funq --save

...or:

$ yarn add funq

Usage

Create a new instance of Funq(). Each object will have a push() method, taking a callback as an argument and sequential() and parallel() methods to kick the whole process off. The constructor function takes a completion callback with an error value which will either contain an error object/string, etc.. or be null if no errors were passed back, this is called once the whole chain of functions have finished.

If parallel() is used, then all functions pushed to the instance will run immediately and the completion callback will be called once the slowest function has finished, otherwise, using sequential() everything will run in order and take as long as all the functions take to complete. Also you can optionally pass a value back (i.e. done([value])) which will complete the sequence early.

Example:

const Funq = require('funq')
 
const queue = new Funq((err, value) => {
  if (err) return console.log(err) // an error was passed back and the sequence ended early 
  // otherwise do stuff after sequence is fully completed 
})
 
queue.push((fail, done) => {
  // do some async work and either fail with an error 
  // or mark successfull with done 
 
  // optionally pass a value back, this also has the effect 
  // or completing the sequence early 
  done({pass: 'value back'})
})
 
queue.push((fail, done) => {
  fail(new Error('an error occurred'))
})
 
queue.push((fail, done) => {
  // won't be called because error was passed back 
})
 
queue.sequential() // or `.parallel()` 

Browsers

Use browserify or similar.