callback-sequence
Advanced tools
Comparing version 1.1.0 to 1.2.0
54
index.js
@@ -1,31 +0,45 @@ | ||
var runTask = require('orchestrator/lib/runTask'); | ||
var bindAsync = require('run-callback').bindAsync; | ||
var noop = function () {}; | ||
var undef; | ||
module.exports = sequence; | ||
module.exports.run = run; | ||
module.exports.last = {}; | ||
function sequence() { | ||
var callbacks = [].concat.apply([], arguments); | ||
return function (done) { | ||
run(callbacks, done); | ||
}; | ||
return run.bind(null, arguments); | ||
} | ||
function run(callbacks, done) { | ||
function run(things, initial, done) { | ||
var res = []; | ||
if (arguments.length < 3) { | ||
done = initial; | ||
initial = undef; | ||
} | ||
done = done || noop; | ||
if (callbacks.length === 0) { | ||
return done(); | ||
} | ||
var cb = callbacks[0]; | ||
if (typeof cb === 'function') { | ||
runTask(cb, next); | ||
} else { | ||
next(null); | ||
} | ||
function next(err) { | ||
if (err) { | ||
return done(err); | ||
(function NEXT(i, len) { | ||
if (i >= len) { | ||
return done(null, res); | ||
} | ||
run(callbacks.slice(1), done); | ||
} | ||
bind(things[i], res, initial)(function (err, r) { | ||
if (err) { | ||
return done(err, res); | ||
} | ||
res.push(r); | ||
NEXT(++i, len); | ||
}); | ||
}(0, things.length)); | ||
} | ||
function bind(fn, res, initial) { | ||
var args = [].concat(fn).map(function (j) { | ||
if (j === sequence.last) { | ||
return res.length === 0 ? initial : res[res.length - 1]; | ||
} | ||
return j; | ||
}); | ||
return bindAsync.apply(null, args); | ||
} | ||
{ | ||
"name": "callback-sequence", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Make a new callback to run input callbacks in sequence", | ||
@@ -27,3 +27,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"orchestrator": "^0.3.7" | ||
"run-callback": "^1.0.0" | ||
}, | ||
@@ -30,0 +30,0 @@ "devDependencies": { |
# callback-sequence | ||
Make a new callback to run input callbacks in sequence | ||
Make a new callback to run callbacks in sequence. | ||
It is meant to make it easy to construct a [gulp](https://npmjs.org/package/gulp) task from a sequence of callbacks. | ||
Callbacks can be made async like [gulp tasks](https://github.com/gulpjs/gulp/blob/master/docs/API.md#fn). | ||
## Usage | ||
# Usage | ||
@@ -36,20 +36,95 @@ ```javascript | ||
## cb = sequence() | ||
# API | ||
Each argument passed in could be a [gulp task callback](https://github.com/gulpjs/gulp/blob/master/docs/API.md#gulptaskname-deps-fn), | ||
or an array containing such elements. | ||
## cb = sequence(task1, task2,...) | ||
`sequence` will create a callback to run all those specified tasks in appearance order. | ||
`cb` has signature `cb(done)`, and `done` is called after those tasks finish, | ||
with an error object or `null`. | ||
## sequence.run(callbacks, done) | ||
### cb([initial,] done) | ||
#### initial | ||
Type: `mixed` | ||
*Optional* | ||
If specified, it can be passed to the first task through `sequence.last`. | ||
See [task](#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. | ||
```javascript | ||
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` | ||
An array of [gulp task callback](https://github.com/gulpjs/gulp/blob/master/docs/API.md#gulptaskname-deps-fn)s. | ||
Elements are [tasks](#task). | ||
`done`, if specified, is called after all tasks finish, | ||
with an error object or `null`. | ||
```javascript | ||
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); | ||
}); | ||
``` | ||
## results | ||
Type: `Array` | ||
Store all the results of the tasks. | ||
It is passed to [done](#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`. | ||
Sorry, the diff of this file is not supported yet
5771
38
130
+ Addedrun-callback@^1.0.0
+ Addedarrayify-slice@1.0.0(transitive)
+ Addedend-of-stream@1.4.4(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedrun-callback@1.1.0(transitive)
- Removedorchestrator@^0.3.7
- Removedend-of-stream@0.1.5(transitive)
- Removedonce@1.3.3(transitive)
- Removedorchestrator@0.3.8(transitive)
- Removedsequencify@0.0.7(transitive)