JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 104
  • Score
    100M100P100Q73987F
  • License MIT

Make a new callback to run input callbacks in sequence

Package Exports

  • callback-sequence

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (callback-sequence) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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