You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

callback-sequence

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

callback-sequence

Make a new callback to run input callbacks in sequence

1.3.1
Source
npmnpm
Version published
Weekly downloads
65
-83.38%
Maintainers
1
Weekly downloads
 
Created
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

gulp

FAQs

Package last updated on 15 Oct 2015

Did you know?

Socket

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