callback-sequence
Make a new callback to run callbacks in sequence or parallel.
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(
sync, [async, promise], stream
))
gulp.task('sequence-nested', 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')
})
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) {
})
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) {
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')
})