1 |
|
2 |
|
3 | var setTimeoutFunc = setTimeout;
|
4 |
|
5 | function noop() {}
|
6 |
|
7 |
|
8 | function bind(fn, thisArg) {
|
9 | return function() {
|
10 | fn.apply(thisArg, arguments);
|
11 | };
|
12 | }
|
13 |
|
14 | function Promise(fn) {
|
15 | if (!(this instanceof Promise))
|
16 | throw new TypeError('Promises must be constructed via new');
|
17 | if (typeof fn !== 'function') throw new TypeError('not a function');
|
18 | this._state = 0;
|
19 | this._handled = false;
|
20 | this._value = undefined;
|
21 | this._deferreds = [];
|
22 |
|
23 | doResolve(fn, this);
|
24 | }
|
25 |
|
26 | function handle(self, deferred) {
|
27 | while (self._state === 3) {
|
28 | self = self._value;
|
29 | }
|
30 | if (self._state === 0) {
|
31 | self._deferreds.push(deferred);
|
32 | return;
|
33 | }
|
34 | self._handled = true;
|
35 | Promise._immediateFn(function() {
|
36 | var cb = self._state === 1 ? deferred.onFulfilled : deferred.onRejected;
|
37 | if (cb === null) {
|
38 | (self._state === 1 ? resolve : reject)(deferred.promise, self._value);
|
39 | return;
|
40 | }
|
41 | var ret;
|
42 | try {
|
43 | ret = cb(self._value);
|
44 | } catch (e) {
|
45 | reject(deferred.promise, e);
|
46 | return;
|
47 | }
|
48 | resolve(deferred.promise, ret);
|
49 | });
|
50 | }
|
51 |
|
52 | function resolve(self, newValue) {
|
53 | try {
|
54 |
|
55 | if (newValue === self)
|
56 | throw new TypeError('A promise cannot be resolved with itself.');
|
57 | if (
|
58 | newValue &&
|
59 | (typeof newValue === 'object' || typeof newValue === 'function')
|
60 | ) {
|
61 | var then = newValue.then;
|
62 | if (newValue instanceof Promise) {
|
63 | self._state = 3;
|
64 | self._value = newValue;
|
65 | finale(self);
|
66 | return;
|
67 | } else if (typeof then === 'function') {
|
68 | doResolve(bind(then, newValue), self);
|
69 | return;
|
70 | }
|
71 | }
|
72 | self._state = 1;
|
73 | self._value = newValue;
|
74 | finale(self);
|
75 | } catch (e) {
|
76 | reject(self, e);
|
77 | }
|
78 | }
|
79 |
|
80 | function reject(self, newValue) {
|
81 | self._state = 2;
|
82 | self._value = newValue;
|
83 | finale(self);
|
84 | }
|
85 |
|
86 | function finale(self) {
|
87 | if (self._state === 2 && self._deferreds.length === 0) {
|
88 | Promise._immediateFn(function() {
|
89 | if (!self._handled) {
|
90 | Promise._unhandledRejectionFn(self._value);
|
91 | }
|
92 | });
|
93 | }
|
94 |
|
95 | for (var i = 0, len = self._deferreds.length; i < len; i++) {
|
96 | handle(self, self._deferreds[i]);
|
97 | }
|
98 | self._deferreds = null;
|
99 | }
|
100 |
|
101 | function Handler(onFulfilled, onRejected, promise) {
|
102 | this.onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : null;
|
103 | this.onRejected = typeof onRejected === 'function' ? onRejected : null;
|
104 | this.promise = promise;
|
105 | }
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 | function doResolve(fn, self) {
|
114 | var done = false;
|
115 | try {
|
116 | fn(
|
117 | function(value) {
|
118 | if (done) return;
|
119 | done = true;
|
120 | resolve(self, value);
|
121 | },
|
122 | function(reason) {
|
123 | if (done) return;
|
124 | done = true;
|
125 | reject(self, reason);
|
126 | }
|
127 | );
|
128 | } catch (ex) {
|
129 | if (done) return;
|
130 | done = true;
|
131 | reject(self, ex);
|
132 | }
|
133 | }
|
134 |
|
135 | Promise.prototype['catch'] = function(onRejected) {
|
136 | return this.then(null, onRejected);
|
137 | };
|
138 |
|
139 | Promise.prototype.then = function(onFulfilled, onRejected) {
|
140 | var prom = new this.constructor(noop);
|
141 |
|
142 | handle(this, new Handler(onFulfilled, onRejected, prom));
|
143 | return prom;
|
144 | };
|
145 |
|
146 | Promise.all = function(arr) {
|
147 | return new Promise(function(resolve, reject) {
|
148 | if (!arr || typeof arr.length === 'undefined')
|
149 | throw new TypeError('Promise.all accepts an array');
|
150 | var args = Array.prototype.slice.call(arr);
|
151 | if (args.length === 0) return resolve([]);
|
152 | var remaining = args.length;
|
153 |
|
154 | function res(i, val) {
|
155 | try {
|
156 | if (val && (typeof val === 'object' || typeof val === 'function')) {
|
157 | var then = val.then;
|
158 | if (typeof then === 'function') {
|
159 | then.call(
|
160 | val,
|
161 | function(val) {
|
162 | res(i, val);
|
163 | },
|
164 | reject
|
165 | );
|
166 | return;
|
167 | }
|
168 | }
|
169 | args[i] = val;
|
170 | if (--remaining === 0) {
|
171 | resolve(args);
|
172 | }
|
173 | } catch (ex) {
|
174 | reject(ex);
|
175 | }
|
176 | }
|
177 |
|
178 | for (var i = 0; i < args.length; i++) {
|
179 | res(i, args[i]);
|
180 | }
|
181 | });
|
182 | };
|
183 |
|
184 | Promise.resolve = function(value) {
|
185 | if (value && typeof value === 'object' && value.constructor === Promise) {
|
186 | return value;
|
187 | }
|
188 |
|
189 | return new Promise(function(resolve) {
|
190 | resolve(value);
|
191 | });
|
192 | };
|
193 |
|
194 | Promise.reject = function(value) {
|
195 | return new Promise(function(resolve, reject) {
|
196 | reject(value);
|
197 | });
|
198 | };
|
199 |
|
200 | Promise.race = function(values) {
|
201 | return new Promise(function(resolve, reject) {
|
202 | for (var i = 0, len = values.length; i < len; i++) {
|
203 | values[i].then(resolve, reject);
|
204 | }
|
205 | });
|
206 | };
|
207 |
|
208 |
|
209 | Promise._immediateFn =
|
210 | (typeof setImmediate === 'function' &&
|
211 | function(fn) {
|
212 | setImmediate(fn);
|
213 | }) ||
|
214 | function(fn) {
|
215 | setTimeoutFunc(fn, 0);
|
216 | };
|
217 |
|
218 | Promise._unhandledRejectionFn = function _unhandledRejectionFn(err) {
|
219 | if (typeof console !== 'undefined' && console) {
|
220 | console.warn('Possible Unhandled Promise Rejection:', err);
|
221 | }
|
222 | };
|
223 |
|
224 | export default Promise;
|