-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
237 lines (223 loc) · 6.09 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*!
* each-promise <https://github.com/tunnckoCore/each-promise>
*
* Copyright (c) Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
* Released under the MIT license.
*/
'use strict'
var Promize = require('native-or-another')
var utils = require('./utils')
var eachPromise = {}
/**
* > 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 eachPromise = require('each-promise')
*
* var arr = [
* () => delay(500).then(() => 1),
* () => delay(200).then(() => { throw Error('foo') }),
* () => delay(10).then(() => 3),
* () => delay(350).then(() => 4),
* () => delay(150).then(() => 5)
* ]
*
* eachPromise
* .serial(arr)
* .then((res) => {
* console.log(res) // [1, Error: foo, 3, 4, 5]
* })
*
* // see what happens when parallel
* eachPromise
* .parallel(arr)
* .then((res) => {
* console.log(res) // => [3, 5, Error: foo, 4, 1]
* })
*
* // pass `settle: false` if you want
* // to stop after first error
* eachPromise
* .serial(arr, { settle: false })
* .catch((err) => console.log(err)) // => Error: foo
* ```
*
* @name .serial
* @param {Array} `<iterable>` iterable object like array 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} Always resolved or rejected promise
* @api public
*/
eachPromise.serial = function eachSerial (iterable, mapper, opts) {
var options = utils.defaults(mapper, opts)
options = utils.extend(options, {
serial: true
})
return eachPromise.each(iterable, options.mapper, options)
}
/**
* > 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 eachPromise = require('each-promise')
*
* 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
* eachPromise
* .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} `<iterable>` iterable object like array 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} Always resolved or rejected promise
* @api public
*/
eachPromise.parallel = function eachParallel (iterable, mapper, opts) {
var options = utils.defaults(mapper, opts)
return eachPromise.each(iterable, options.mapper, utils.extend(options, {
serial: false
}))
}
/**
* > 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 eachPromise = require('each-promise')
*
* var arr = [
* 123,
* function () {
* return delay(500).then(() => 456)
* },
* Promise.resolve(678),
* function () {
* return 999
* },
* function () {
* return delay(200).then(() => 'foo')
* }
* ]
*
* eachPromise
* .each(arr)
* .then(function (res) {
* console.log('done', res) // => [123, 678, 999, 'foo', 456]
* })
* ```
*
* @name .each
* @param {Array} `<iterable>` iterable object like array 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} Always resolved or rejected promise
* @api public
*/
eachPromise.each = function each (iterable, mapper, opts) {
if (typeof iterable !== 'object') {
var err = new TypeError('expect `iterable` to be array, iterable or object')
return Promize.reject(err)
}
var options = utils.defaults(mapper, opts)
return promiseEach(iterable, options)
}
/**
* > Base iterate logic
*
* @param {Array|Object} `<iterable>`
* @param {Object} `[opts]`
* @return {Promise}
* @api private
*/
function promiseEach (iterable, opts) {
return new opts.Promise(function (resolve, reject) {
var results = []
var arr = Array.isArray(iterable) ? iterable : []
arr.doneCount = 0
if (!arr.length && typeof iterable === 'object') {
for (var key in iterable) {
arr.push(iterable[key])
}
}
opts.concurrency = opts.serial === false ? opts.concurrency : 1
opts.concurrency = opts.concurrency || arr.length
opts.start()
if (!opts.serial) {
for (var index = 0; index < opts.concurrency; index++) {
utils.iterator(arr, results)(opts, resolve, reject)(index)
}
return
}
utils.iterator(arr, results)(opts, resolve, reject)(0)
})
}
module.exports = eachPromise