-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
53 lines (43 loc) · 1.22 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
'use strict';
var Oath = require('./oath');
Oath.all = function oathAll(oaths) {
return new Oath(function(res, rej) {
var accumulator = new Array(oaths.length);
var numResolved = 0;
function onResolveFactory(index) {
return function onResolve(resVal) {
accumulator[index] = resVal;
numResolved++;
if(numResolved === oaths.length) {
res(accumulator);
}
};
}
function onReject(error) {
rej(error);
}
oaths.forEach(function(currOath, index) {
currOath.then(onResolveFactory(index));
currOath.catch(onReject);
});
});
};
Oath.race = function oathRace(oaths) {
return new Oath(function(res, rej) {
var numRejected = 0;
function onReject(error) {
numRejected++;
if(numRejected === oaths.length) {
rej(error);
}
}
function onResolve(resVal) {
res(resVal);
}
oaths.forEach(function(currOath) {
currOath.then(onResolve);
currOath.catch(onReject);
});
});
};
module.exports = Oath;