forked from leangjia/mymind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
promise.js
130 lines (104 loc) · 3.45 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
/*
Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
*/
/**
* @class A promise - value to be resolved in the future.
* Implements the "Promises/A+" specification.
*/
var Promise = function() {
this._state = 0; /* 0 = pending, 1 = fulfilled, 2 = rejected */
this._value = null; /* fulfillment / rejection value */
this._cb = {
fulfilled: [],
rejected: []
}
this._thenPromises = []; /* promises returned by then() */
}
/**
* @param {function} onFulfilled To be called once this promise gets fulfilled
* @param {function} onRejected To be called once this promise gets rejected
* @returns {Promise}
*/
Promise.prototype.then = function(onFulfilled, onRejected) {
this._cb.fulfilled.push(onFulfilled);
this._cb.rejected.push(onRejected);
var thenPromise = new Promise();
this._thenPromises.push(thenPromise);
if (this._state > 0) {
setTimeout(this._processQueue.bind(this), 0);
}
/* 3.2.6. then must return a promise. */
return thenPromise;
}
/**
* Fulfill this promise with a given value
* @param {any} value
*/
Promise.prototype.fulfill = function(value) {
if (this._state != 0) { return this; }
this._state = 1;
this._value = value;
this._processQueue();
return this;
}
/**
* Reject this promise with a given value
* @param {any} value
*/
Promise.prototype.reject = function(value) {
if (this._state != 0) { return this; }
this._state = 2;
this._value = value;
this._processQueue();
return this;
}
/**
* Pass this promise's resolved value to another promise
* @param {Promise} promise
*/
Promise.prototype.chain = function(promise) {
return this.then(promise.fulfill.bind(promise), promise.reject.bind(promise));
}
/**
* @param {function} onRejected To be called once this promise gets rejected
* @returns {Promise}
*/
Promise.prototype["catch"] = function(onRejected) {
return this.then(null, onRejected);
}
Promise.prototype._processQueue = function() {
while (this._thenPromises.length) {
var onFulfilled = this._cb.fulfilled.shift();
var onRejected = this._cb.rejected.shift();
this._executeCallback(this._state == 1 ? onFulfilled : onRejected);
}
}
Promise.prototype._executeCallback = function(cb) {
var thenPromise = this._thenPromises.shift();
if (typeof(cb) != "function") {
if (this._state == 1) {
/* 3.2.6.4. If onFulfilled is not a function and promise1 is fulfilled, promise2 must be fulfilled with the same value. */
thenPromise.fulfill(this._value);
} else {
/* 3.2.6.5. If onRejected is not a function and promise1 is rejected, promise2 must be rejected with the same reason. */
thenPromise.reject(this._value);
}
return;
}
try {
var returned = cb(this._value);
if (returned && typeof(returned.then) == "function") {
/* 3.2.6.3. If either onFulfilled or onRejected returns a promise (call it returnedPromise), promise2 must assume the state of returnedPromise */
var fulfillThenPromise = function(value) { thenPromise.fulfill(value); }
var rejectThenPromise = function(value) { thenPromise.reject(value); }
returned.then(fulfillThenPromise, rejectThenPromise);
} else {
/* 3.2.6.1. If either onFulfilled or onRejected returns a value that is not a promise, promise2 must be fulfilled with that value. */
thenPromise.fulfill(returned);
}
} catch (e) {
/* 3.2.6.2. If either onFulfilled or onRejected throws an exception, promise2 must be rejected with the thrown exception as the reason. */
thenPromise.reject(e);
}
}