-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (55 loc) · 1.47 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
54
55
56
57
58
59
60
61
62
63
64
65
'use strict';
function EnhancedPromise(executor) {
var resolve = function() {};
var reject = function() {};
this.reset = function(executor) {
var onFulfilled = resolve;
var onRejected = reject;
this.promise = new Promise(
function(_resolve, _reject) {
resolve = _resolve;
reject = _reject;
if (executor) {
executor(this.resolve, this.reject, this.reset);
}
}.bind(this)
);
this.promise.then(onFulfilled, onRejected);
}.bind(this);
this.resolve = function(value) {
return resolve(value);
};
this.reject = function(reason) {
return reject(reason);
};
this.then = function(onFulfilled, onRejected) {
return this.promise.then(onFulfilled, onRejected);
};
this.catch = function(onRejected) {
return this.promise.catch(onRejected);
};
this.finally = function(onFinally) {
return this.promise.finally(onFinally);
};
this.reset(executor);
}
EnhancedPromise.resolve = function(value) {
return new EnhancedPromise(function(resolve) {
resolve(value);
});
};
EnhancedPromise.reject = function(reason) {
return new EnhancedPromise(function(resolve, reject) {
reject(reason);
});
};
EnhancedPromise.deferred = function() {
return new EnhancedPromise();
};
EnhancedPromise.all = function() {
return Promise.all.apply(Promise, arguments);
};
EnhancedPromise.race = function() {
return Promise.race.apply(Promise, arguments);
};
module.exports = EnhancedPromise;