forked from dima117/shri-async-hw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise-functions.js
56 lines (53 loc) · 1.12 KB
/
promise-functions.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
Promise._any = function(promises) {
return new Promise((resolve) => {
Promise.race(promises)
.then((value) => {
resolve(value);
})
.catch((reason) => {
console.log(reason);
});
});
};
Promise._allSettled = function(promises) {
let length = promises.length;
let count = 0;
const result = [];
return new Promise((resolve, _) => {
for(let i = 0; i < length; i++) {
promises[i]
.then((value) => {
count++;
result.push({
status: 'fullfilled',
value
});
})
.catch((reason) => {
count++;
result.push({
status: 'rejected',
reason
});
});
}
const interval = setInterval(() => {
if (count === length) {
clearInterval(interval);
resolve(result);
}
}, 20);
});
};
Promise.prototype._finally = function(callback) {
const that = this;
return new Promise((resolve, reject) => {
that.then(() => {
callback();
resolve();
}).catch(() => {
callback();
resolve()
});
});
};