-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
promise.js
91 lines (80 loc) · 2.3 KB
/
promise.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
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
if ('Promise' in globalThis) {
if (! (Promise.prototype.finally instanceof Function)) {
Promise.prototype.finally = function(callback) {
return this.then(async val => {
await callback();
return val;
}, async val => {
await callback();
return val;
});
};
}
if (! (Promise.allSettled instanceof Function)) {
Promise.allSettled = function(promises) {
return Promise.all(Array.from(promises).map(function(call) {
return new Promise(function(resolve) {
if (! (call instanceof Promise)) {
call = Promise.resolve(call);
}
call.then(function(value) {
resolve({ status: 'fulfilled', value: value });
}).catch(function(reason) {
resolve({ status: 'rejected', reason: reason });
});
});
}));
};
}
if (! (Promise.any instanceof Function)) {
Promise.any = (promises) => new Promise((resolve, reject) => {
let errors = [];
promises.forEach(promise => {
promise.then(resolve).catch(e => {
errors.push(e);
if (errors.length === promises.length) {
reject(new globalThis.AggregateError(errors, 'No Promise in Promise.any was resolved'));
}
});
});
});
}
if (! (Promise.race instanceof Function)) {
Promise.race = (promises) => new Promise((resolve, reject) => {
promises.forEach(promise => promise.then(resolve, reject));
});
}
if (! (Promise.try instanceof Function)) {
/**
* @see https://github.com/tc39/proposal-promise-try
*/
Promise.try = callback => new Promise(resolve => resolve(callback()));
}
if (! (Promise.withResolvers instanceof Function)) {
Promise.withResolvers = function() {
const def = {};
def.promise = new Promise((resolve, reject) => {
def.resolve = resolve;
def.reject = reject;
});
return def;
};
}
if (! (Promise.fromEntries instanceof Function)) {
Promise.fromEntries = async function fromEntries(entries) {
const keys = [];
const values = [];
for (const [key, value] of entries) {
keys.push(key);
values.push(value);
}
return await Promise.all(values)
.then(values => Object.fromEntries(values.map((value, i) => [keys[i], value])));
};
}
if (!(Promise.ownProperties instanceof Function)) {
Promise.ownProperties = async function ownProperties(obj) {
return await Promise.fromEntries(Object.entries(obj));
};
}
}