forked from chenjunxyf/mypromise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmypromise.js
139 lines (129 loc) · 2.85 KB
/
mypromise.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
var PENDING = 0;
var FULFILLED = 1;
var REJECTED = 2;
function Promise(fn) {
// store state which can be PENDING, FULFILLED or REJECTED
var state = PENDING;
// store value once FULFILLED or REJECTED
var value = null;
// store sucess & failure handlers
var handlers = [];
function fulfill(result) {
state = FULFILLED;
value = result;
handlers.forEach(handle);
handlers = null;
}
function reject(error) {
state = REJECTED;
value = error;
handlers.forEach(handle);
handlers = null;
}
function resolve(result) {
try {
var then = getThen(result);
// it is also a promise object
if (then) {
doResolve(then.bind(result), resolve, reject);
return;
}
fulfill(result);
} catch(ex) {
reject(ex);
}
}
function handle(handler) {
if (state === PENDING) {
handlers.push(handler);
} else {
if (state === FULFILLED && typeof handler.onFulfilled === 'function') {
handler.onFulfilled(value);
}
if (state === REJECTED && typeof handler.onRejected === 'function') {
handler.onRejected(value);
}
}
}
this.done = function(onFulfilled, onRejected) {
// ensure we are always asynchronous
setTimeout(function() {
handle({
onFulfilled: onFulfilled,
onRejected: onRejected
});
}, 0);
};
this.then = function(onFulfilled, onRejected) {
var self = this;
return new Promise(function(resolve, reject) {
return self.done(function(result) {
if (typeof onFulfilled === 'function') {
try {
return resolve(onFulfilled(result));
} catch(ex) {
return reject(ex);
}
} else {
return resolve(result);
}
}, function(error) {
if (typeof onRejected === 'function') {
try {
return resolve(onRejected(error));
} catch(ex) {
return reject(ex);
}
} else {
return reject(error);
}
});
});
};
doResolve(fn, resolve, reject);
}
/**
* Check if a value is a Promise and, if it is,
* return the `then` method of that promise.
*
* @param {Promise|Any} value
* @return {Function|Null}
*/
function getThen(value) {
var t = typeof value;
if (value && (t === 'object' || t === 'function')) {
var then = value.then;
if (typeof then === 'function') {
return then;
}
}
return null;
}
/**
* Take a potentially misbehaving resolver function and make sure
* onFulfilled and onRejected are only called once.
*
* Makes no guarantees about asynchrony.
*
* @param {Function} fn A resolver function that may not be trusted
* @param {Function} onFulfilled
* @param {Function} onRejected
*/
function doResolve(fn, onFulfilled, onRejected) {
var done = false;
try {
fn(function(value) {
if (done) return;
done = true;
onFulfilled(value);
}, function(reason) {
if (done) return;
done = true;
onRejected(reason);
});
} catch(ex) {
if (done) return;
done = true;
onRejected(ex);
}
}