index.js
2.26 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
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
'use strict';
// Load modules
// Declare internals
const internals = {};
exports.serial = function (array, method, callback) {
if (!array.length) {
callback();
}
else {
let i = 0;
const iterate = function () {
const done = function (err) {
if (err) {
callback(err);
}
else {
i = i + 1;
if (i < array.length) {
iterate();
}
else {
callback();
}
}
};
method(array[i], done, i);
};
iterate();
}
};
exports.parallel = function (array, method, callback) {
if (!array.length) {
callback();
}
else {
let count = 0;
let errored = false;
const done = function (err) {
if (!errored) {
if (err) {
errored = true;
callback(err);
}
else {
count = count + 1;
if (count === array.length) {
callback();
}
}
}
};
for (let i = 0; i < array.length; ++i) {
method(array[i], done, i);
}
}
};
exports.parallel.execute = function (fnObj, callback) {
const result = {};
if (!fnObj) {
return callback(null, result);
}
const keys = Object.keys(fnObj);
let count = 0;
let errored = false;
if (!keys.length) {
return callback(null, result);
}
const done = function (key) {
return function (err, val) {
if (!errored) {
if (err) {
errored = true;
callback(err);
}
else {
result[key] = val;
if (++count === keys.length) {
callback(null, result);
}
}
}
};
};
for (let i = 0; i < keys.length; ++i) {
if (!errored) {
const key = keys[i];
fnObj[key](done(key));
}
}
};