1
1
# callback-sequence
2
- Make a new callback to run callbacks in sequence or parallel.
3
-
4
2
[ ![ version] ( https://img.shields.io/npm/v/callback-sequence.svg )] ( https://www.npmjs.org/package/callback-sequence )
5
3
[ ![ status] ( https://travis-ci.org/zoubin/callback-sequence.svg?branch=master )] ( https://travis-ci.org/zoubin/callback-sequence )
6
4
[ ![ dependencies] ( https://david-dm.org/zoubin/callback-sequence.svg )] ( https://david-dm.org/zoubin/callback-sequence )
7
5
[ ![ devDependencies] ( https://david-dm.org/zoubin/callback-sequence/dev-status.svg )] ( https://david-dm.org/zoubin/callback-sequence#info=devDependencies )
8
6
7
+ Make a new callback to run callbacks in sequence or parallel.
8
+
9
9
Callbacks can be made async like [ gulp tasks] ( https://github.com/gulpjs/gulp/blob/master/docs/API.md#fn ) .
10
10
11
11
## Example
12
12
13
13
``` javascript
14
- var sequence = require (' callback-sequence' )
14
+ var thunkify = require (' callback-sequence' )
15
+
15
16
var Readable = require (' stream' ).Readable
16
17
var gulp = require (' gulp' )
17
18
18
- gulp .task (' sequence' , sequence (
19
+ gulp .task (' sequence' , thunkify (
19
20
sync, async , promise, stream
20
21
))
21
22
22
- gulp .task (' parallel' , sequence (
23
+ gulp .task (' parallel' , thunkify (
23
24
[sync, async , promise, stream]
24
25
))
25
26
26
- gulp .task (' parallel-nested' , sequence (
27
+ gulp .task (' parallel-nested' , thunkify (
27
28
// `async` and `promise` will be run in parallel
28
29
sync, [async , promise], stream
29
30
))
30
31
31
- gulp .task (' sequence-nested' , sequence (
32
+ gulp .task (' sequence-nested' , thunkify (
32
33
// `async` and `promise` will be run in sequence
33
34
[sync, [async , promise], stream]
34
35
))
@@ -54,143 +55,191 @@ function stream() {
54
55
55
56
## API
56
57
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.
59
60
60
61
` cb ` will return a promise.
61
62
62
63
``` 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
+ })
64
85
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 ) },
67
99
[
68
100
function (cb ) {
69
101
setTimeout (function () {
70
- console . log (3 )
102
+ res . push (3 )
71
103
cb ()
72
104
}, 0 )
73
105
},
74
106
function () {
75
107
return new Promise (function (resolve ) {
76
108
process .nextTick (function () {
77
- console . log (2 )
109
+ res . push (2 )
78
110
resolve ()
79
111
})
80
112
})
81
113
},
82
114
],
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)
121
121
})
122
122
123
123
```
124
124
125
- Actually, you can add callbacks dynamically:
125
+ Callbacks an be added dynamically:
126
126
127
127
``` javascript
128
128
var run = require (' callback-sequence' ).run
129
129
130
130
var count = 5
131
131
var tasks = []
132
132
133
- function task (res , next ) {
133
+ var res = []
134
+ function task (next ) {
134
135
process .nextTick (function () {
135
136
res .push (count)
136
137
if (-- count > 0 ) {
137
138
tasks .push (task)
138
139
}
139
- next (null , res )
140
+ next ()
140
141
})
141
142
}
142
- run (tasks, [[]] ).then (function (res ) {
143
- // [ [ 5, 4, 3, 2, 1] ]
143
+ run (tasks).then (function () {
144
+ // [5, 4, 3, 2, 1]
144
145
console .log (res)
145
146
})
146
147
147
148
tasks .push (task)
148
149
149
150
```
150
151
151
- ### res = sequence .parallel(tasks, initialArgs )
152
+ ### thunkify .parallel(tasks)
152
153
Run the specified tasks in parallel.
153
154
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.
157
156
158
157
``` javascript
159
158
var parallel = require (' callback-sequence' ).parallel
160
159
160
+ var res = []
161
161
parallel ([
162
- function () { console . log (1 ) },
162
+ function () { res . push (1 ) },
163
163
[
164
- function (cb ) {
165
- setTimeout (function () {
166
- console .log (3 )
167
- cb ()
168
- }, 0 )
169
- },
170
164
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 )
176
167
})
177
168
},
169
+ function () { res .push (5 ) },
178
170
],
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
+ },
180
181
]
181
182
)
182
183
.then (function () {
183
- console .log (' DONE' )
184
+ // [1, 2, 4, 5, 3]
185
+ console .log (res)
184
186
})
185
187
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
+ })
191
233
192
234
193
235
```
194
236
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
+
195
244
## [ Changelog] ( changelog.md )
196
245
0 commit comments