JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 104
  • Score
    100M100P100Q73949F
  • 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 or parallel.

version status dependencies devDependencies

Callbacks can be made async like gulp tasks.

Example

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

gulp.task('sequence', sequence(
  sync, async, promise, stream
))

gulp.task('parallel', sequence(
  [sync, async, promise, stream]
))

gulp.task('parallel-nested', sequence(
  // `async` and `promise` will be run in parallel
  sync, [async, promise], stream
))

gulp.task('sequence-nested', sequence(
  // `async` and `promise` will be run in sequence
  [sync, [async, promise], stream]
))

function sync() {
}

function async(cb) {
  process.nextTick(cb)
}

function promise() {
  return Promise.resolve()
}

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

API

cb = sequence(...tasks)

Return a callback to run the specified tasks in appearance order.

cb will return a promise.

var sequence = require('callback-sequence')

sequence(
  function () { console.log(1) },
  [
    function (cb) {
      setTimeout(function() {
        console.log(3)
        cb()
      }, 0)
    },
    function () {
      return new Promise(function (resolve) {
        process.nextTick(function () {
          console.log(2)
          resolve()
        })
      })
    },
  ],
  function () { console.log(4) },
)().then(function () {
  console.log('DONE')
})

// 1
// 2
// 3
// 4
// DONE

res = sequence.run(tasks, initialArgs)

Run the specified tasks in sequence.

  • tasks: Type: Array. If a task is specified as an array of subtasks, those tasks will be run with sequence.parallel
  • initialArgs: Type: Array. Arguments passed to the first task.
  • res: Type: Promise. Resolves to an array of results created by the last task.
var sequence = require('callback-sequence')

run([
  function (a, b) {
    t.same([a, b], [1, 2])
    return a + b
  },
  function (res, cb) {
    t.same(res, 3)
    setTimeout(function() {
      cb(null, res, 4)
    }, 0)
  },
], [1, 2])
.then(function (res) {
  // [3, 4]
})

Actually, you can add callbacks dynamically:

var run = require('callback-sequence').run

var count = 5
var tasks = []

function task(res, next) {
  process.nextTick(function () {
    res.push(count)
    if (--count > 0) {
      tasks.push(task)
    }
    next(null, res)
  })
}
run(tasks, [[]]).then(function (res) {
  // [ [5, 4, 3, 2, 1] ]
  console.log(res)
})

tasks.push(task)

res = sequence.parallel(tasks, initialArgs)

Run the specified tasks in parallel.

  • tasks: Type: Array. If a task is specified as an array of subtasks, those tasks will be run with sequence.run.
  • initialArgs: Type: Array. Arguments passed to all tasks.
  • res: Type: Promise. Resolves to an array of results created by the call tasks.
var parallel = require('callback-sequence').parallel

parallel([
  function () { console.log(1) },
  [
    function (cb) {
      setTimeout(function() {
        console.log(3)
        cb()
      }, 0)
    },
    function () {
      return new Promise(function (resolve) {
        process.nextTick(function () {
          console.log(2)
          resolve()
        })
      })
    },
  ],
  function () { console.log(4) },
]
)
.then(function () {
  console.log('DONE')
})

// 1
// 4
// 3
// 2
// DONE

Changelog