Socket
Socket
Sign inDemoInstall

callback-sequence

Package Overview
Dependencies
7
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
49.7 kB
Created
Weekly downloads
 

Changelog

Source

v2.0.0 (2015-11-27)

Readme

Source

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

Keywords

FAQs

Last updated on 27 Nov 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