-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
46 lines (40 loc) · 1.25 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
'use strict';
const assert = require('assert');
module.exports = function (funcs, n) {
assert(Array.isArray(funcs), 'funcs must be array');
n = n || 5;
// if funcs contians promise, gather can't limit the concurrency
if (funcs.some(func => func && typeof func.then === 'function')) n = funcs.length;
return new Promise(resolve => {
if (funcs.length === 0) return resolve([]);
const results = [];
let index = 0;
let finish = 0;
for (let i = 0; i < n; i++) {
run();
}
function run() {
const i = index++;
if (i >= funcs.length) return;
let ins = funcs[i];
if (!ins) {
results[i] = undefined;
if (++finish >= funcs.length) return resolve(results);
return;
}
if (typeof ins === 'function') ins = ins();
if (ins && typeof ins.next === 'function' && typeof ins.throw === 'function') ins = require('co')(ins);
ins.then(res => {
results[i] = { value: res, isError: false };
}, err => {
results[i] = { error: err, isError: true };
results[i].isError = true;
results[i].error = err;
})
.then(() => {
if (++finish >= funcs.length) return resolve(results);
run();
});
}
});
};