forked from Azure/login
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_parallel.js
More file actions
59 lines (53 loc) · 1.12 KB
/
Copy path_parallel.js
File metadata and controls
59 lines (53 loc) · 1.12 KB
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
import AsynccError from './AsynccError'
export default function parallel (limit, length, run, opts = {}, callback) {
if (typeof opts === 'function') {
callback = opts
opts = {}
}
limit = Math.abs(limit || length)
const errpos = []
const errors = new Array(length).fill()
const results = new Array(length).fill()
let i = 0
let l = length
let done = 0
if (l === 0) {
final()
return
}
if (opts.timeout) {
setTimeout(() => {
/* istanbul ignore else */
if (l) final('err_timeout')
}, opts.timeout)
}
limit = limit < length ? limit : length
while (i < limit) {
run(i++, cb)
}
function final (errMsg) {
if (done++) return
let err = null
if (errpos.length || errMsg) {
err = new AsynccError(errMsg || 'err', errors, errpos)
}
callback && callback(err, results)
}
function cb (j, err, res) {
results[j] = res
errors[j] = err
if (err) {
errpos.push(j)
if (opts.bail) {
final('err_bail')
return
}
}
l--
if (i < length) {
run(i++, cb)
} else if (!l) {
final()
}
}
}