callback-sequence
Advanced tools
Comparing version 2.0.0 to 3.0.0
@@ -1,54 +0,65 @@ | ||
<!-- LATEST 9a70cfb --> | ||
<!-- a5016b8 1449485237000 --> | ||
## [v3.0.0](https://github.com/zoubin/callback-sequence/commit/a5016b8) (2015-12-07) | ||
* [[`3468c13`](https://github.com/zoubin/callback-sequence/commit/3468c13)] Refactor. | ||
* Exports a `Runner` to specify options. | ||
* Always return a promise. | ||
* Arguments are ignored by default. | ||
* [[`8ddfdfc`](https://github.com/zoubin/callback-sequence/commit/8ddfdfc)] CHANGELOG | ||
## [v2.0.0](https://github.com/zoubin/callback-sequence/commit/9a70cfb) (2015-11-27) | ||
* [ 2015-11-27 [f979b6e](https://github.com/zoubin/callback-sequence/commit/f979b6e) ] Support parallel | ||
* [[`f979b6e`](https://github.com/zoubin/callback-sequence/commit/f979b6e)] Support parallel | ||
* [ 2015-11-27 [441a740](https://github.com/zoubin/callback-sequence/commit/441a740) ] More tests | ||
* [[`441a740`](https://github.com/zoubin/callback-sequence/commit/441a740)] More tests | ||
## [v1.4.0](https://github.com/zoubin/callback-sequence/commit/f34b900) (2015-11-26) | ||
* [ 2015-11-26 [5700fc2](https://github.com/zoubin/callback-sequence/commit/5700fc2) ] Delete npm badge | ||
* [[`5700fc2`](https://github.com/zoubin/callback-sequence/commit/5700fc2)] Delete npm badge | ||
* [ 2015-11-26 [13be3b2](https://github.com/zoubin/callback-sequence/commit/13be3b2) ] Use ES5 | ||
* [[`13be3b2`](https://github.com/zoubin/callback-sequence/commit/13be3b2)] Use ES5 | ||
* [ 2015-11-15 [385dc7e](https://github.com/zoubin/callback-sequence/commit/385dc7e) ] update task-tape, gulp-tape | ||
* [[`385dc7e`](https://github.com/zoubin/callback-sequence/commit/385dc7e)] update task-tape, gulp-tape | ||
## [v1.3.2](https://github.com/zoubin/callback-sequence/commit/2f12901) (2015-10-15) | ||
* [ 2015-10-15 [e704538](https://github.com/zoubin/callback-sequence/commit/e704538) ] fix babel export default problem | ||
* [[`e704538`](https://github.com/zoubin/callback-sequence/commit/e704538)] fix babel export default problem | ||
* [ 2015-10-15 [8b3d322](https://github.com/zoubin/callback-sequence/commit/8b3d322) ] add badges | ||
* [[`8b3d322`](https://github.com/zoubin/callback-sequence/commit/8b3d322)] add badges | ||
## [v1.3.1](https://github.com/zoubin/callback-sequence/commit/4d08890) (2015-10-15) | ||
* [ 2015-10-15 [9c58153](https://github.com/zoubin/callback-sequence/commit/9c58153) ] update run-callback | ||
* [[`9c58153`](https://github.com/zoubin/callback-sequence/commit/9c58153)] update run-callback | ||
* [ 2015-10-06 [d2bcbda](https://github.com/zoubin/callback-sequence/commit/d2bcbda) ] eslint | ||
* [[`d2bcbda`](https://github.com/zoubin/callback-sequence/commit/d2bcbda)] eslint | ||
## [v1.3.0](https://github.com/zoubin/callback-sequence/commit/c4e1854) (2015-10-05) | ||
* [ 2015-10-05 [c61f137](https://github.com/zoubin/callback-sequence/commit/c61f137) ] add callbacks dynamically | ||
* [[`c61f137`](https://github.com/zoubin/callback-sequence/commit/c61f137)] add callbacks dynamically | ||
## [v1.2.1](https://github.com/zoubin/callback-sequence/commit/f59dc05) (2015-09-27) | ||
* [ 2015-09-27 [31cdf83](https://github.com/zoubin/callback-sequence/commit/31cdf83) ] fix initial | ||
* [[`31cdf83`](https://github.com/zoubin/callback-sequence/commit/31cdf83)] fix initial | ||
## [v1.2.0](https://github.com/zoubin/callback-sequence/commit/4eff823) (2015-09-17) | ||
* [ 2015-09-17 [a96f554](https://github.com/zoubin/callback-sequence/commit/a96f554) ] deliver results | ||
* [[`a96f554`](https://github.com/zoubin/callback-sequence/commit/a96f554)] deliver results | ||
## [v1.1.0](https://github.com/zoubin/callback-sequence/commit/2022a99) (2015-09-15) | ||
* [ 2015-09-15 [f6bdbf2](https://github.com/zoubin/callback-sequence/commit/f6bdbf2) ] export method `run` | ||
* [[`f6bdbf2`](https://github.com/zoubin/callback-sequence/commit/f6bdbf2)] export method `run` | ||
* [ 2015-08-12 [51279dd](https://github.com/zoubin/callback-sequence/commit/51279dd) ] add test, eslint | ||
* [[`51279dd`](https://github.com/zoubin/callback-sequence/commit/51279dd)] add test, eslint | ||
* [ 2015-07-31 [5596f29](https://github.com/zoubin/callback-sequence/commit/5596f29) ] readme | ||
* [[`5596f29`](https://github.com/zoubin/callback-sequence/commit/5596f29)] readme | ||
## [v1.0.0](https://github.com/zoubin/callback-sequence/commit/a43744e) (2015-07-31) | ||
* [ 2015-07-31 [86095b5](https://github.com/zoubin/callback-sequence/commit/86095b5) ] construct task callback | ||
* [[`86095b5`](https://github.com/zoubin/callback-sequence/commit/86095b5)] construct task callback | ||
* [ 2015-07-31 [dda0d5c](https://github.com/zoubin/callback-sequence/commit/dda0d5c) ] Initial commit | ||
* [[`dda0d5c`](https://github.com/zoubin/callback-sequence/commit/dda0d5c)] Initial commit | ||
62
index.js
@@ -1,58 +0,8 @@ | ||
var promisify = require('node-promisify') | ||
var runCallback = promisify(require('run-callback'), -1) | ||
var Runner = require('./lib/runner') | ||
var runner = new Runner({ input: false, output: false }) | ||
exports = module.exports = function () { | ||
var cbs = Array.prototype.slice.call(arguments) | ||
return function () { | ||
return run(cbs) | ||
} | ||
} | ||
exports = module.exports = runner.thunkify.bind(runner) | ||
exports.run = exports.sequence = runner.sequence.bind(runner) | ||
exports.parallel = runner.parallel.bind(runner) | ||
exports.Runner = Runner | ||
exports.run = run | ||
exports.parallel = parallel | ||
function run(cbs, args, i) { | ||
i = ~~i | ||
args = defined(args) ? args : [] | ||
// NOTE: tasks can be pushed to `cbs` dynamically | ||
var cb = cbs[i] | ||
var ret | ||
if (typeof cb === 'function') { | ||
++i | ||
ret = runCallback([cb].concat(args)).then(valid) | ||
} else if (Array.isArray(cb)) { | ||
++i | ||
ret = parallel(cb, args) | ||
} else { | ||
ret = Promise.resolve([].concat(args)) | ||
} | ||
return ret.then(function (res) { | ||
if (i < cbs.length) { | ||
return run(cbs, res, i) | ||
} | ||
return res | ||
}) | ||
} | ||
function parallel(cbs, args) { | ||
args = defined(args) ? args : [] | ||
return Promise.all(cbs.map(function (cb) { | ||
if (typeof cb === 'function') { | ||
return runCallback([cb].concat(args)).then(valid) | ||
} | ||
if (Array.isArray(cb)) { | ||
return run(cb, args, 0) | ||
} | ||
return Promise.resolve([].concat(args)) | ||
})) | ||
} | ||
function valid(res) { | ||
return res.filter(defined) | ||
} | ||
function defined(o) { | ||
return typeof o !== 'undefined' | ||
} | ||
{ | ||
"name": "callback-sequence", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"description": "Make a new callback to run input callbacks in sequence", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "npm run lint && task-tape test/*.js | tap-summary", | ||
"lint": "eslint *.js lib/**/*.js test/*.js bin/*.js" | ||
"test": "npm run lint && tap --cov test/*.js", | ||
"lint": "eslint *.js 'lib/**/*.js' test/*.js bin/*.js", | ||
"coveralls": "COVERALLS_REPO_TOKEN=MNaz7xU7XD7Ei8Z09KxbkNQNzzBtQJWyZ npm test" | ||
}, | ||
@@ -28,11 +29,8 @@ "repository": { | ||
"dependencies": { | ||
"node-promisify": "^1.0.0", | ||
"run-callback": "^2.2.0" | ||
"run-callback": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^1.10.1", | ||
"tap-summary": "^1.0.0", | ||
"tape": "^4.2.0", | ||
"task-tape": "^1.0.0" | ||
"tap": "^2.3.1" | ||
} | ||
} |
209
README.md
# callback-sequence | ||
Make a new callback to run callbacks in sequence or parallel. | ||
[![version](https://img.shields.io/npm/v/callback-sequence.svg)](https://www.npmjs.org/package/callback-sequence) | ||
@@ -9,2 +7,4 @@ [![status](https://travis-ci.org/zoubin/callback-sequence.svg?branch=master)](https://travis-ci.org/zoubin/callback-sequence) | ||
Make a new callback to run callbacks in sequence or parallel. | ||
Callbacks can be made async like [gulp tasks](https://github.com/gulpjs/gulp/blob/master/docs/API.md#fn). | ||
@@ -15,15 +15,16 @@ | ||
```javascript | ||
var sequence = require('callback-sequence') | ||
var thunkify = require('callback-sequence') | ||
var Readable = require('stream').Readable | ||
var gulp = require('gulp') | ||
gulp.task('sequence', sequence( | ||
gulp.task('sequence', thunkify( | ||
sync, async, promise, stream | ||
)) | ||
gulp.task('parallel', sequence( | ||
gulp.task('parallel', thunkify( | ||
[sync, async, promise, stream] | ||
)) | ||
gulp.task('parallel-nested', sequence( | ||
gulp.task('parallel-nested', thunkify( | ||
// `async` and `promise` will be run in parallel | ||
@@ -33,3 +34,3 @@ sync, [async, promise], stream | ||
gulp.task('sequence-nested', sequence( | ||
gulp.task('sequence-nested', thunkify( | ||
// `async` and `promise` will be run in sequence | ||
@@ -60,4 +61,4 @@ [sync, [async, promise], stream] | ||
### cb = sequence(...tasks) | ||
Return a callback to run the specified tasks in appearance order. | ||
### cb = thunkify(...tasks) | ||
Return a callback to run the specified tasks in the appearance order. | ||
@@ -67,10 +68,41 @@ `cb` will return a promise. | ||
```javascript | ||
var sequence = require('callback-sequence') | ||
var res = [] | ||
thunkify( | ||
function () { | ||
res.push(1) | ||
}, | ||
function (next) { | ||
process.nextTick(function () { | ||
res.push(2) | ||
next() | ||
}) | ||
}, | ||
function () { | ||
return Promise.resolve().then(function () { | ||
res.push(3) | ||
}) | ||
} | ||
)() | ||
.then(function () { | ||
// [1, 2, 3] | ||
console.log(res) | ||
}) | ||
sequence( | ||
function () { console.log(1) }, | ||
``` | ||
### thunkify.run(tasks) | ||
It just runs `tasks` like you call the function returned by `thunkify` | ||
**NOTE**: if some task is an array of sub-tasks, they will be run in parallel. | ||
```javascript | ||
var run = require('callback-sequence').run | ||
var res = [] | ||
run([ | ||
function () { res.push(1) }, | ||
[ | ||
function (cb) { | ||
setTimeout(function() { | ||
console.log(3) | ||
res.push(3) | ||
cb() | ||
@@ -82,3 +114,3 @@ }, 0) | ||
process.nextTick(function () { | ||
console.log(2) | ||
res.push(2) | ||
resolve() | ||
@@ -89,47 +121,15 @@ }) | ||
], | ||
function () { console.log(4) }, | ||
)().then(function () { | ||
console.log('DONE') | ||
function () { res.push(4) }, | ||
] | ||
) | ||
.then(function () { | ||
// [1, 2, 3, 4] | ||
console.log(res) | ||
}) | ||
// 1 | ||
// 2 | ||
// 3 | ||
// 4 | ||
// DONE | ||
``` | ||
### res = sequence.run(tasks, initialArgs) | ||
Run the specified tasks in sequence. | ||
Callbacks an be added dynamically: | ||
* `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. | ||
```javascript | ||
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: | ||
```javascript | ||
var run = require('callback-sequence').run | ||
@@ -140,3 +140,4 @@ | ||
function task(res, next) { | ||
var res = [] | ||
function task(next) { | ||
process.nextTick(function () { | ||
@@ -147,7 +148,7 @@ res.push(count) | ||
} | ||
next(null, res) | ||
next() | ||
}) | ||
} | ||
run(tasks, [[]]).then(function (res) { | ||
// [ [5, 4, 3, 2, 1] ] | ||
run(tasks).then(function () { | ||
// [5, 4, 3, 2, 1] | ||
console.log(res) | ||
@@ -160,8 +161,6 @@ }) | ||
### res = sequence.parallel(tasks, initialArgs) | ||
### thunkify.parallel(tasks) | ||
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. | ||
**NOTE**: if some task is an array of sub-tasks, they will be run in sequence. | ||
@@ -171,37 +170,87 @@ ```javascript | ||
var res = [] | ||
parallel([ | ||
function () { console.log(1) }, | ||
function () { res.push(1) }, | ||
[ | ||
function (cb) { | ||
setTimeout(function() { | ||
console.log(3) | ||
cb() | ||
}, 0) | ||
}, | ||
function () { | ||
return new Promise(function (resolve) { | ||
process.nextTick(function () { | ||
console.log(2) | ||
resolve() | ||
}) | ||
return Promise.resolve().then(function () { | ||
res.push(4) | ||
}) | ||
}, | ||
function () { res.push(5) }, | ||
], | ||
function () { console.log(4) }, | ||
function (cb) { | ||
setTimeout(function() { | ||
res.push(3) | ||
cb() | ||
}, 0) | ||
}, | ||
function (cb) { | ||
res.push(2) | ||
cb() | ||
}, | ||
] | ||
) | ||
.then(function () { | ||
console.log('DONE') | ||
// [1, 2, 4, 5, 3] | ||
console.log(res) | ||
}) | ||
// 1 | ||
// 4 | ||
// 3 | ||
// 2 | ||
// DONE | ||
``` | ||
### Runner = thunkify.Runner(opts) | ||
Return a new runner instance, with the following methods: | ||
* `sequence`: just like `thunkify.run` | ||
* `parallel`: just like `thunkify.parallel` | ||
* `thunkify`: just like `thunkify` | ||
#### opts | ||
##### input | ||
Specify whether to pass the results of the previous callback to the next as arguments. | ||
Type: `Boolean` | ||
Default: `true` | ||
```javascript | ||
var Runner = require('callback-sequence').Runner | ||
var runner = Runner({ input: true }) | ||
runner.thunkify( | ||
function (a, b) { | ||
// 3 | ||
return a + b | ||
}, | ||
function (sum, next) { | ||
process.nextTick(function () { | ||
// 6 | ||
next(null, sum * 2) | ||
}) | ||
}, | ||
function (product) { | ||
return Promise.resolve().then(function () { | ||
// 7 | ||
return product + 1 | ||
}) | ||
} | ||
)(1, 2) | ||
.then(function (res) { | ||
// [7] | ||
console.log(res) | ||
}) | ||
``` | ||
##### output | ||
Specify whether to pass the results of the last callback to the final results. | ||
Type: `Boolean` | ||
Default: `true` | ||
## [Changelog](changelog.md) | ||
10172
1
2
5
73
246
+ Addedrun-callback@3.1.0(transitive)
- Removednode-promisify@^1.0.0
- Removedmixy@1.0.0(transitive)
- Removednode-promisify@1.2.0(transitive)
- Removedrun-callback@2.2.0(transitive)
Updatedrun-callback@^3.0.0