-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
215 lines (205 loc) · 6.11 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*!
* minibase-control-flow <https://github.com/node-minibase/minibase-control-flow>
*
* Copyright (c) Charlike Mike Reagent <@tunnckoCore> (http://i.am.charlike.online)
* Released under the MIT license.
*/
'use strict'
var utils = require('./utils')
/**
* > Adds `.serial` and `.parallel` methods to your application.
* The `opts` object is merged with app's options and it is
* passed to respective [each-promise][] methods.
* See [options section](#options).
*
* **Example**
*
* ```js
* var flow = require('minibase-control-flow')
*
* var MiniBase = require('minibase').MiniBase
* var app = new MiniBase()
* app.use(flow())
*
* // or as Base plugin
*
* var Base = require('base')
* var base = new Base()
* base.use(flow())
* ```
*
* @param {Object} `opts` optional, passed directly to [each-promise][]
* @return {Function} plugin that can be passed to [base][]/[minibase][]'s `.use` method
* @api public
*/
module.exports = function minibaseControlFlow (opts) {
return utils.createPlugin('minibase-control-flow', function (self) {
self.options = utils.extend(self.options, opts)
/**
* > Iterate over `iterable` in series (serially)
* with optional `opts` (see [options section](#options))
* and optional `mapper` function (see [item section](#item)).
*
* **Example**
*
* ```js
* var delay = require('delay')
* var flow = require('minibase-control-flow')
* var app = require('minibase')
*
* app.use(flow())
*
* var arr = [
* () => delay(500).then(() => 1),
* () => delay(200).then(() => { throw Error('foo') }),
* () => delay(10).then(() => 3),
* () => delay(350).then(() => 4),
* () => delay(150).then(() => 5)
* ]
*
* app.serial(arr)
* .then((res) => {
* console.log(res) // [1, Error: foo, 3, 4, 5]
* })
*
* // see what happens when parallel
* app.parallel(arr)
* .then((res) => {
* console.log(res) // => [3, 5, Error: foo, 4, 1]
* })
*
* // pass `settle: false` if you want
* // to stop after first error
* app.serial(arr, { settle: false })
* .catch((err) => console.log(err)) // => Error: foo
* ```
*
* @name .serial
* @param {Array|Object} `<iterable>` iterable object like array or object with any type of values
* @param {Function} `[mapper]` function to apply to each item in `iterable`, see [item section](#item)
* @param {Object} `[opts]` see [options section](#options)
* @return {Promise}
* @api public
*/
self.define('serial', utils.wrap(self, utils.each.serial))
/**
* > Iterate concurrently over `iterable` in parallel (support limiting with `opts.concurrency`)
* with optional `opts` (see [options section](#options))
* and optional `mapper` function (see [item section](#item)).
*
* **Example**
*
* ```js
* var flow = require('minibase-control-flow')
* var app = require('minibase')
*
* app.use(flow())
*
* var arr = [
* function one () {
* return delay(200).then(() => {
* return 123
* })
* },
* Promise.resolve('foobar'),
* function two () {
* return delay(1500).then(() => {
* return 345
* })
* },
* delay(10).then(() => 'zero'),
* function three () {
* return delay(400).then(() => {
* coffffnsole.log(3) // eslint-disable-line no-undef
* return 567
* })
* },
* 'abc',
* function four () {
* return delay(250).then(() => {
* return 789
* })
* },
* function five () {
* return delay(100).then(() => {
* sasasa // eslint-disable-line no-undef
* return 444
* })
* },
* function six () {
* return delay(80).then(() => {
* return 'last'
* })
* }
* ]
*
* // does not stop after first error
* // pass `settle: false` if you want
* app.parallel(arr).then((res) => {
* console.log(res)
* // => [
* // 'foobar',
* // 'abc',
* // 'zero',
* // 'last',
* // ReferenceError: sasasa is not defined,
* // 123,
* // 789,
* // ReferenceError: coffffnsole is not defined
* // 345
* // ]
* })
* ```
*
* @name .parallel
* @param {Array|Object} `<iterable>` iterable object like array or object with any type of values
* @param {Function} `[mapper]` function to apply to each item in `iterable`, see [item section](#item)
* @param {Object} `[opts]` see [options section](#options)
* @return {Promise}
* @api public
*/
self.define('parallel', utils.wrap(self, utils.each.parallel))
/**
* > Iterate over `iterable` in series or parallel (default), depending on
* default `opts`. Pass `opts.serial: true` if you
* want to iterate in series, pass `opts.serial: false` or does not
* pass anything for parallel.
*
* **Example**
*
* ```js
* var delay = require('delay')
* var app = require('minibase')
* var flow = require('minibase-control-flow')
*
* app.use(flow())
*
* var promise = app.each([
* 123,
* function () {
* return delay(500).then(() => 456)
* },
* Promise.resolve(678),
* function () {
* return 999
* },
* function () {
* return delay(200).then(() => 'foo')
* }
* ])
*
* promise.then(function (res) {
* console.log('done', res) // => [123, 678, 999, 'foo', 456]
* })
* ```
*
* @name .each
* @param {Array|Object} `<iterable>` iterable object like array or object with any type of values
* @param {Function} `[mapper]` function to apply to each item in `iterable`, see [item section](#item)
* @param {Object} `[opts]` see [options section](#options)
* @return {Promise}
* @api public
*/
self.define('each', utils.wrap(self, utils.each.each))
})
}