-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpromise.js
executable file
·150 lines (131 loc) · 3.68 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
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
140
141
142
143
144
145
146
147
148
149
150
var PENDING = 0,
FULFILLED = 1,
REJECTED = 2;
function isFunction(fn) {
return fn instanceof Function;
}
function isObject(obj) {
return obj instanceof Object;
}
function isPromise(p) {
return p instanceof Promise;
}
function isThenable(obj) {
return obj && isFunction(obj.then);
}
function callLater(fn) {
setTimeout(fn, 0);
}
function Promise(executor) {
var that = this;
that.status = PENDING;
that.value = undefined;
that.handlerQueue = [];
executor(function(value) {
that.transition(FULFILLED, value);
}, function(reason) {
that.transition(REJECTED, reason);
});
}
Promise.prototype.transition = function(status, value) {
if (this.status === PENDING) {
this.status = status;
this.value = value;
this.process();
}
};
Promise.prototype.process = function() {
var that = this;
if (that.status === PENDING) {
return;
}
while (that.handlerQueue.length > 0) {
var handler = that.handlerQueue.shift();
(function(handler) {
var handlerFn = that.status === FULFILLED ? handler.onFulfilled :
handler.onRejected;
if (isFunction(handlerFn)) {
callLater(function() {
try {
var x = handlerFn(that.value);
resolve(handler.thenPromise, x);
} catch (e) {
handler.thenPromise.transition(REJECTED,
e);
}
});
} else {
handler.thenPromise.transition(that.status, that.value);
}
})(handler);
}
};
function resolve(promise, x) {
if (promise === x) {
promise.transition(REJECTED, new TypeError());
} else if (isPromise(x)) {
x.then(function(value) {
promise.transition(FULFILLED, value);
}, function(reason) {
promise.transition(REJECTED, reason);
});
} else if (isObject(x) || isFunction(x)) {
try {
var then = x.then;
if (isFunction(then)) {
var called = false;
try {
then.call(x, function(y) {
if (!called) {
resolve(promise, y);
called = true;
}
}, function(r) {
if (!called) {
promise.transition(REJECTED, r);
called = true;
}
});
} catch (e) {
if (!called) {
promise.transition(REJECTED, e);
}
}
} else {
promise.transition(FULFILLED, x);
}
} catch (e) {
promise.transition(REJECTED, e);
}
} else {
promise.transition(FULFILLED, x);
}
};
Promise.resolve = function(value) {
return new Promise(function(resolve, reject) {
if (isThenable(value)) {
value.then(resolve, reject);
}
else {
resolve(value);
}
});
};
Promise.reject = function(reason) {
return new Promise(function(resolve, reject) {
reject(reason);
});
};
Promise.prototype.then = function(onFulfilled, onRejected) {
var thenPromise = new Promise(function() {});
this.handlerQueue.push({
onFulfilled: onFulfilled,
onRejected: onRejected,
thenPromise: thenPromise
});
this.process();
return thenPromise;
};
if (module && module.exports) {
module.exports = Promise;
}