Socket
Socket
Sign inDemoInstall

callback-sequence

Package Overview
Dependencies
5
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    callback-sequence

Make a new callback to run input callbacks in sequence


Version published
Weekly downloads
29
increased by16%
Maintainers
1
Install size
29.0 kB
Created
Weekly downloads
 

Changelog

Source

v1.3.1 (2015-10-15)

Readme

Source

callback-sequence

Make a new callback to run callbacks in sequence.

npm

version status dependencies devDependencies

Callbacks can be made async like gulp tasks.

Usage

var sequence = require('callback-sequence');
var Readable = require('stream').Readable;
var gulp = require('gulp');

gulp.task('publish', sequence(
  read, lint, warn, bump
));

function lint() {
}

function warn(cb) {
  process.nextTick(cb);
}

function bump() {
  return Promise.resolve();
}

function read() {
  var s = Readable();
  s.push(null);
  return s;
}

API

cb = sequence(task1, task2,...)

sequence will create a callback to run all those specified tasks in appearance order.

cb([initial,] done)

initial

Type: mixed

Optional

If specified, it can be passed to the first task through sequence.last. See task.

done

Type: Function Signature: done(err, results)

done is called after all tasks finish.

results is an array containing all results of the tasks.

task

Type: Function, Array

If Array, the first element is treated as the callback, and elements following the callback are passed to it as arguments.

var sequence = require('callback-sequence');

function sum(a, b, next) {
  process.nextTick(function () {
    next(null, a + b);
  });
}
sequence(
  [sum, sequence.last, 1],
  [sum, sequence.last, 1],
  [sum, sequence.last, 1]
)(1, function (err, res) {
  console.log('Expected:', [2, 3, 4]);
  console.log('Actual:', res);
});

sequence.run(callbacks, [initial, ] done)

callbacks

Type: Array

Elements are tasks.

var sequence = require('callback-sequence');

function sum(a, b, next) {
  process.nextTick(function () {
    next(null, a + b);
  });
}
sequence.run([
  [sum, sequence.last, 1],
  [sum, sequence.last, 1],
  [sum, sequence.last, 1],
], 1, function (err, res) {
  console.log('Expected:', [2, 3, 4]);
  console.log('Actual:', res);
});

Actually, you can dynamically add callbacks:

var sequence = require('callback-sequence');

var tasks = [task];
var count = 0;
function task(next) {
  process.nextTick(function () {
    count++;
    if (count < 5) {
      tasks.push(task);
    }
    next(null, count);
  });
}
sequence.run(tasks, function (err, res) {
  console.log(res);
  // [ 1, 2, 3, 4, 5 ]
});

results

Type: Array

Store all the results of the tasks.

It is passed to done as the second argument.

Sync callbacks deliver results with return,

async callbacks with the last argument passed to it (next(err, res)),

promisified callbacks with resolve(res),

and streamified callbacks always deliver undefined.

Changelog

Keywords

FAQs

Last updated on 15 Oct 2015

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc