This repository has been archived by the owner on May 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
comical-promise.js
283 lines (254 loc) · 8.49 KB
/
comical-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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
var ComicalPromise = function () {
// Valid Promise states
var PENDING = 0;
var FULFILLED = 1;
var REJECTED = 2;
// Promise data
var _state = PENDING;
var _value = undefined;
var _reason = undefined;
var _onFulfilledCallbacks = [];
var _onRejectedCallbacks = [];
/**
* @param {any} value
*/
function _fulfill(value) {
if (_state === PENDING) {
_value = value;
_state = FULFILLED;
_onFulfilledCallbacks.forEach(function(cb) {
callLater(function() { cb(_value) });
});
}
}
/**
* @param {any} reason
*/
function _reject(reason) {
if (_state === PENDING) {
_reason = reason;
_state = REJECTED;
_onRejectedCallbacks.forEach(function(cb) {
callLater(function() { cb(_reason) });
});
}
}
/**
* @param {string} callbackType
* @param {function} callback
*/
function _queueResolutionCallback(callbackType, callback) {
// About `callLater`: Used to execute the callback at a time when
// the execution stack contains only platform code.
if (_state === PENDING) {
if (callbackType === "onFulfilled") {
_onFulfilledCallbacks.push(function(result) {
callLater(function() {
callback(result);
});
});
}
else if (callbackType === "onRejected") {
_onRejectedCallbacks.push(function(result) {
callLater(function() {
callback(result);
});
});
}
else {
throw new Error("Unrecognized callback type");
}
}
else if (_state === FULFILLED) {
if (callbackType === "onFulfilled") {
callLater(function() {
callback(_value);
});
}
}
else if (_state === REJECTED) {
if (callbackType === "onRejected") {
callLater(function() {
callback(_reason);
});
}
}
else {
throw new Error("Unrecognized state" + _state);
}
}
/**
* @param {function} onFulfilled
* @param {function} onRejected
*/
function _then(onFulfilled, onRejected) {
var promise2 = ComicalPromise();
if (!isFunction(onFulfilled)) {
// If `onFulfilled` is not a function and `promise1` is fulfilled,
// `promise2` must be fulfilled with the same value as `promise1`.
_queueResolutionCallback("onFulfilled", function(value) {
promise2.fulfill(value);
});
}
else {
_queueResolutionCallback("onFulfilled", function(value) {
try {
// If either `onFulfilled` or `onRejected` returns a value `x`,
// run the Promise Resolution Procedure [[Resolve]](promise2, x).
resolve(promise2, onFulfilled(value));
}
catch (e) {
// If either `onFulfilled` or `onRejected` throws an exception `e`,
// `promise2` must be rejected with `e` as the reason.
promise2.reject(e);
}
});
}
if (!isFunction(onRejected)) {
// If `onRejected` is not a function and `promise1` is rejected,
// `promise2` must be rejected with the same reason as `promise1`.
_queueResolutionCallback("onRejected", function(reason) {
promise2.reject(reason);
});
}
else {
_queueResolutionCallback("onRejected", function(reason) {
try {
// If either `onFulfilled` or `onRejected` returns a value `x`,
// run the Promise Resolution Procedure [[Resolve]](promise2, x).
resolve(promise2, onRejected(reason));
}
catch (e) {
// If either `onFulfilled` or `onRejected` throws an exception `e`,
// `promise2` must be rejected with `e` as the reason.
promise2.reject(e);
}
});
}
// `then` must return a promise
return promise2;
}
return {
fulfill: _fulfill,
reject: _reject,
then: _then,
comical: true // sentinel to signal that this is a indeed a comical promise
};
};
/**
* Promise Resolution Procedure
*
* @param {ComicalPromise} promise
* @param {any} x
*/
function resolve(promise, x) {
// If `promise` and `x` refer to the same object,
// reject `promise` with a TypeError as the reason.
if (promise === x) {
promise.reject(TypeError("Cannot resolve a promsie with iteself"));
}
// If `x` is a promise, adopt its state
else if (isPromise(x)) {
x.then(function(value) {
promise.fulfill(value);
}, function(reason) {
promise.reject(reason);
});
}
// Otherwise, if `x` is an object or function
else if (isObject(x) || isFunction(x)) {
var then;
try {
// let `then` be `x.then`
then = x.then
} catch (e) {
// If retrieving the property `x.then` results in a thrown exception `e`,
// reject promise with `e` as the reason.
promise.reject(e);
return;
}
// if `then` is a function, call it with `x` as `this`,
// first argument `resolvePromise`, and second argument `rejectPromise`
if (isFunction(then)) {
// If both `resolvePromise` and `rejectPromise` are called,
// or multiple calls to the same argument are made,
// the first call takes precedence, and any further calls are ignored.
var handlerHasBeenCalled = false;
try {
then.call(
x,
function resolvePromise(y) {
if (!handlerHasBeenCalled) {
// If/when `resolvePromise` is called with a value `y`,
// run [[Resolve]](promise, y).
handlerHasBeenCalled = true;
resolve(promise, y);
}
},
function rejectPromise(r) {
if (!handlerHasBeenCalled) {
// If/when `rejectPromise` is called with a reason `r`,
// reject promise with r.
handlerHasBeenCalled = true;
promise.reject(r);
}
}
);
}
// If calling then throws an exception e,
catch (e) {
// If `resolvePromise` or `rejectPromise` have been called, ignore it.
if (!handlerHasBeenCalled) {
// Otherwise, reject promise with `e` as the reason.
promise.reject(e);
}
}
}
else {
// If `then` is not a function, fulfill promise with `x`.
promise.fulfill(x);
}
}
else {
// If `x` is not an object or function, fulfill promise with `x`.
promise.fulfill(x)
}
}
ComicalPromise.resolve = resolve;
var test = ComicalPromise();
console.log(test);
test.then(function(res) {
console.log(res);
});
test.fulfill("Hello");
// Helpers
function isTruthy(thing) {
return !!(thing);
}
function isFunction(thing) {
return isTruthy(thing) && typeof thing === "function";
}
function isObject(thing) {
return isTruthy(thing) && typeof thing === "object";
}
function isUndefined(thing) {
return typeof thing === "undefined";
}
function isPromise(thing) {
return isTruthy(thing) && isTruthy(thing.comical);
}
function callLater(func) {
setTimeout(func, 0);
}
module.exports.deferred = function() {
var p = ComicalPromise();
return {
promise: p,
resolve: function(value) {
ComicalPromise.resolve(p, value);
},
reject: function(reason) {
p.reject(reason);
}
};
};