Skip to content

Commit 3468c13

Browse files
committed
Refactor.
* Exports a `Runner` to specify options. * Always return a promise. * Arguments are ignored by default.
1 parent 8ddfdfc commit 3468c13

22 files changed

+1019
-446
lines changed

.eslintrc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,12 @@ rules:
2828
use-isnan: 2
2929
valid-typeof: 2
3030
no-unexpected-multiline: 2
31-
no-cond-assign: 2
3231
no-constant-condition: 2
3332
no-control-regex: 2
3433
no-debugger: 2
3534
# code style
3635
consistent-return: 0
37-
curly: 2
36+
curly: [2, "multi-line"]
3837
default-case: 2
3938
dot-notation: 2
4039
dot-location: [2, "property"]

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
npm-debug.log
44
build/
55
coverage/
6+
.nyc_output/

.npmignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
npm-debug.log
22
changelog.md
3-
coverage
4-
example
5-
test
3+
coverage/
4+
example/
5+
test/
66
.*

README.md

Lines changed: 129 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,35 @@
11
# callback-sequence
2-
Make a new callback to run callbacks in sequence or parallel.
3-
42
[![version](https://img.shields.io/npm/v/callback-sequence.svg)](https://www.npmjs.org/package/callback-sequence)
53
[![status](https://travis-ci.org/zoubin/callback-sequence.svg?branch=master)](https://travis-ci.org/zoubin/callback-sequence)
64
[![dependencies](https://david-dm.org/zoubin/callback-sequence.svg)](https://david-dm.org/zoubin/callback-sequence)
75
[![devDependencies](https://david-dm.org/zoubin/callback-sequence/dev-status.svg)](https://david-dm.org/zoubin/callback-sequence#info=devDependencies)
86

7+
Make a new callback to run callbacks in sequence or parallel.
8+
99
Callbacks can be made async like [gulp tasks](https://github.com/gulpjs/gulp/blob/master/docs/API.md#fn).
1010

1111
## Example
1212

1313
```javascript
14-
var sequence = require('callback-sequence')
14+
var thunkify = require('callback-sequence')
15+
1516
var Readable = require('stream').Readable
1617
var gulp = require('gulp')
1718

18-
gulp.task('sequence', sequence(
19+
gulp.task('sequence', thunkify(
1920
sync, async, promise, stream
2021
))
2122

22-
gulp.task('parallel', sequence(
23+
gulp.task('parallel', thunkify(
2324
[sync, async, promise, stream]
2425
))
2526

26-
gulp.task('parallel-nested', sequence(
27+
gulp.task('parallel-nested', thunkify(
2728
// `async` and `promise` will be run in parallel
2829
sync, [async, promise], stream
2930
))
3031

31-
gulp.task('sequence-nested', sequence(
32+
gulp.task('sequence-nested', thunkify(
3233
// `async` and `promise` will be run in sequence
3334
[sync, [async, promise], stream]
3435
))
@@ -54,143 +55,191 @@ function stream() {
5455

5556
## API
5657

57-
### cb = sequence(...tasks)
58-
Return a callback to run the specified tasks in appearance order.
58+
### cb = thunkify(...tasks)
59+
Return a callback to run the specified tasks in the appearance order.
5960

6061
`cb` will return a promise.
6162

6263
```javascript
63-
var sequence = require('callback-sequence')
64+
var res = []
65+
thunkify(
66+
function () {
67+
res.push(1)
68+
},
69+
function (next) {
70+
process.nextTick(function () {
71+
res.push(2)
72+
next()
73+
})
74+
},
75+
function () {
76+
return Promise.resolve().then(function () {
77+
res.push(3)
78+
})
79+
}
80+
)()
81+
.then(function () {
82+
// [1, 2, 3]
83+
console.log(res)
84+
})
6485

65-
sequence(
66-
function () { console.log(1) },
86+
```
87+
88+
### thunkify.run(tasks)
89+
It just runs `tasks` like you call the function returned by `thunkify`
90+
91+
**NOTE**: if some task is an array of sub-tasks, they will be run in parallel.
92+
93+
```javascript
94+
var run = require('callback-sequence').run
95+
96+
var res = []
97+
run([
98+
function () { res.push(1) },
6799
[
68100
function (cb) {
69101
setTimeout(function() {
70-
console.log(3)
102+
res.push(3)
71103
cb()
72104
}, 0)
73105
},
74106
function () {
75107
return new Promise(function (resolve) {
76108
process.nextTick(function () {
77-
console.log(2)
109+
res.push(2)
78110
resolve()
79111
})
80112
})
81113
},
82114
],
83-
function () { console.log(4) },
84-
)().then(function () {
85-
console.log('DONE')
86-
})
87-
88-
// 1
89-
// 2
90-
// 3
91-
// 4
92-
// DONE
93-
94-
95-
```
96-
97-
### res = sequence.run(tasks, initialArgs)
98-
Run the specified tasks in sequence.
99-
100-
* `tasks`: Type: `Array`. If a task is specified as an array of subtasks, those tasks will be run with `sequence.parallel`
101-
* `initialArgs`: Type: `Array`. Arguments passed to the first task.
102-
* `res`: Type: `Promise`. Resolves to an array of results created by the last task.
103-
104-
```javascript
105-
var sequence = require('callback-sequence')
106-
107-
run([
108-
function (a, b) {
109-
t.same([a, b], [1, 2])
110-
return a + b
111-
},
112-
function (res, cb) {
113-
t.same(res, 3)
114-
setTimeout(function() {
115-
cb(null, res, 4)
116-
}, 0)
117-
},
118-
], [1, 2])
119-
.then(function (res) {
120-
// [3, 4]
115+
function () { res.push(4) },
116+
]
117+
)
118+
.then(function () {
119+
// [1, 2, 3, 4]
120+
console.log(res)
121121
})
122122

123123
```
124124

125-
Actually, you can add callbacks dynamically:
125+
Callbacks an be added dynamically:
126126

127127
```javascript
128128
var run = require('callback-sequence').run
129129

130130
var count = 5
131131
var tasks = []
132132

133-
function task(res, next) {
133+
var res = []
134+
function task(next) {
134135
process.nextTick(function () {
135136
res.push(count)
136137
if (--count > 0) {
137138
tasks.push(task)
138139
}
139-
next(null, res)
140+
next()
140141
})
141142
}
142-
run(tasks, [[]]).then(function (res) {
143-
// [ [5, 4, 3, 2, 1] ]
143+
run(tasks).then(function () {
144+
// [5, 4, 3, 2, 1]
144145
console.log(res)
145146
})
146147

147148
tasks.push(task)
148149

149150
```
150151

151-
### res = sequence.parallel(tasks, initialArgs)
152+
### thunkify.parallel(tasks)
152153
Run the specified tasks in parallel.
153154

154-
* `tasks`: Type: `Array`. If a task is specified as an array of subtasks, those tasks will be run with `sequence.run`.
155-
* `initialArgs`: Type: `Array`. Arguments passed to all tasks.
156-
* `res`: Type: `Promise`. Resolves to an array of results created by the call tasks.
155+
**NOTE**: if some task is an array of sub-tasks, they will be run in sequence.
157156

158157
```javascript
159158
var parallel = require('callback-sequence').parallel
160159

160+
var res = []
161161
parallel([
162-
function () { console.log(1) },
162+
function () { res.push(1) },
163163
[
164-
function (cb) {
165-
setTimeout(function() {
166-
console.log(3)
167-
cb()
168-
}, 0)
169-
},
170164
function () {
171-
return new Promise(function (resolve) {
172-
process.nextTick(function () {
173-
console.log(2)
174-
resolve()
175-
})
165+
return Promise.resolve().then(function () {
166+
res.push(4)
176167
})
177168
},
169+
function () { res.push(5) },
178170
],
179-
function () { console.log(4) },
171+
function (cb) {
172+
setTimeout(function() {
173+
res.push(3)
174+
cb()
175+
}, 0)
176+
},
177+
function (cb) {
178+
res.push(2)
179+
cb()
180+
},
180181
]
181182
)
182183
.then(function () {
183-
console.log('DONE')
184+
// [1, 2, 4, 5, 3]
185+
console.log(res)
184186
})
185187

186-
// 1
187-
// 4
188-
// 3
189-
// 2
190-
// DONE
188+
```
189+
190+
### Runner = thunkify.Runner(opts)
191+
Return a new runner instance, with the following methods:
192+
193+
* `sequence`: just like `thunkify.run`
194+
* `parallel`: just like `thunkify.parallel`
195+
* `thunkify`: just like `thunkify`
196+
197+
#### opts
198+
199+
##### input
200+
Specify whether to pass the results of the previous callback to the next as arguments.
201+
202+
Type: `Boolean`
203+
204+
Default: `true`
205+
206+
```javascript
207+
var Runner = require('callback-sequence').Runner
208+
209+
var runner = Runner({ input: true })
210+
211+
runner.thunkify(
212+
function (a, b) {
213+
// 3
214+
return a + b
215+
},
216+
function (sum, next) {
217+
process.nextTick(function () {
218+
// 6
219+
next(null, sum * 2)
220+
})
221+
},
222+
function (product) {
223+
return Promise.resolve().then(function () {
224+
// 7
225+
return product + 1
226+
})
227+
}
228+
)(1, 2)
229+
.then(function (res) {
230+
// [7]
231+
console.log(res)
232+
})
191233

192234

193235
```
194236

237+
##### output
238+
Specify whether to pass the results of the last callback to the final results.
239+
240+
Type: `Boolean`
241+
242+
Default: `true`
243+
195244
## [Changelog](changelog.md)
196245

example/dynamic.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@ var run = require('..').run
33
var count = 5
44
var tasks = []
55

6-
function task(res, next) {
6+
var res = []
7+
function task(next) {
78
process.nextTick(function () {
89
res.push(count)
910
if (--count > 0) {
1011
tasks.push(task)
1112
}
12-
next(null, res)
13+
next()
1314
})
1415
}
15-
run(tasks, [[]]).then(function (res) {
16+
run(tasks).then(function () {
17+
// [5, 4, 3, 2, 1]
1618
console.log(res)
1719
})
1820

example/input.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var Runner = require('..').Runner
2+
3+
var runner = Runner({ input: true })
4+
5+
runner.thunkify(
6+
function (a, b) {
7+
// 3
8+
return a + b
9+
},
10+
function (sum, next) {
11+
process.nextTick(function () {
12+
// 6
13+
next(null, sum * 2)
14+
})
15+
},
16+
function (product) {
17+
return Promise.resolve().then(function () {
18+
// 7
19+
return product + 1
20+
})
21+
}
22+
)(1, 2)
23+
.then(function (res) {
24+
// [7]
25+
console.log(res)
26+
})
27+

0 commit comments

Comments
 (0)