]>
Commit | Line | Data |
---|---|---|
152c3995 SR |
1 | /* Copyright (c) 2014 Taylor Hakes |
2 | * Copyright (c) 2014 Forbes Lindesay | |
3 | * | |
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
5 | * of this software and associated documentation files (the "Software"), to deal | |
6 | * in the Software without restriction, including without limitation the rights | |
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
8 | * copies of the Software, and to permit persons to whom the Software is | |
9 | * furnished to do so, subject to the following conditions: | |
10 | * | |
11 | * The above copyright notice and this permission notice shall be included in | |
12 | * all copies or substantial portions of the Software. | |
13 | * | |
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
20 | * THE SOFTWARE. | |
21 | */ | |
22 | ||
23 | (function (root) { | |
24 | ||
25 | // Store setTimeout reference so promise-polyfill will be unaffected by | |
26 | // other code modifying setTimeout (like sinon.useFakeTimers()) | |
27 | var setTimeoutFunc = setTimeout; | |
28 | ||
29 | function noop() {} | |
30 | ||
31 | // Polyfill for Function.prototype.bind | |
32 | function bind(fn, thisArg) { | |
33 | return function () { | |
34 | fn.apply(thisArg, arguments); | |
35 | }; | |
36 | } | |
37 | ||
38 | function Promise(fn) { | |
39 | if (typeof this !== 'object') throw new TypeError('Promises must be constructed via new'); | |
40 | if (typeof fn !== 'function') throw new TypeError('not a function'); | |
41 | this._state = 0; | |
42 | this._handled = false; | |
43 | this._value = undefined; | |
44 | this._deferreds = []; | |
45 | ||
46 | doResolve(fn, this); | |
47 | } | |
48 | ||
49 | function handle(self, deferred) { | |
50 | while (self._state === 3) { | |
51 | self = self._value; | |
52 | } | |
53 | if (self._state === 0) { | |
54 | self._deferreds.push(deferred); | |
55 | return; | |
56 | } | |
57 | self._handled = true; | |
58 | Promise._immediateFn(function () { | |
59 | var cb = self._state === 1 ? deferred.onFulfilled : deferred.onRejected; | |
60 | if (cb === null) { | |
61 | (self._state === 1 ? resolve : reject)(deferred.promise, self._value); | |
62 | return; | |
63 | } | |
64 | var ret; | |
65 | try { | |
66 | ret = cb(self._value); | |
67 | } catch (e) { | |
68 | reject(deferred.promise, e); | |
69 | return; | |
70 | } | |
71 | resolve(deferred.promise, ret); | |
72 | }); | |
73 | } | |
74 | ||
75 | function resolve(self, newValue) { | |
76 | try { | |
77 | // Promise Resolution Procedure: https://github.com/promises-aplus/promises-spec#the-promise-resolution-procedure | |
78 | if (newValue === self) throw new TypeError('A promise cannot be resolved with itself.'); | |
79 | if (newValue && (typeof newValue === 'object' || typeof newValue === 'function')) { | |
80 | var then = newValue.then; | |
81 | if (newValue instanceof Promise) { | |
82 | self._state = 3; | |
83 | self._value = newValue; | |
84 | finale(self); | |
85 | return; | |
86 | } else if (typeof then === 'function') { | |
87 | doResolve(bind(then, newValue), self); | |
88 | return; | |
89 | } | |
90 | } | |
91 | self._state = 1; | |
92 | self._value = newValue; | |
93 | finale(self); | |
94 | } catch (e) { | |
95 | reject(self, e); | |
96 | } | |
97 | } | |
98 | ||
99 | function reject(self, newValue) { | |
100 | self._state = 2; | |
101 | self._value = newValue; | |
102 | finale(self); | |
103 | } | |
104 | ||
105 | function finale(self) { | |
106 | if (self._state === 2 && self._deferreds.length === 0) { | |
107 | Promise._immediateFn(function() { | |
108 | if (!self._handled) { | |
109 | Promise._unhandledRejectionFn(self._value); | |
110 | } | |
111 | }); | |
112 | } | |
113 | ||
114 | for (var i = 0, len = self._deferreds.length; i < len; i++) { | |
115 | handle(self, self._deferreds[i]); | |
116 | } | |
117 | self._deferreds = null; | |
118 | } | |
119 | ||
120 | function Handler(onFulfilled, onRejected, promise) { | |
121 | this.onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : null; | |
122 | this.onRejected = typeof onRejected === 'function' ? onRejected : null; | |
123 | this.promise = promise; | |
124 | } | |
125 | ||
126 | /** | |
127 | * Take a potentially misbehaving resolver function and make sure | |
128 | * onFulfilled and onRejected are only called once. | |
129 | * | |
130 | * Makes no guarantees about asynchrony. | |
131 | */ | |
132 | function doResolve(fn, self) { | |
133 | var done = false; | |
134 | try { | |
135 | fn(function (value) { | |
136 | if (done) return; | |
137 | done = true; | |
138 | resolve(self, value); | |
139 | }, function (reason) { | |
140 | if (done) return; | |
141 | done = true; | |
142 | reject(self, reason); | |
143 | }); | |
144 | } catch (ex) { | |
145 | if (done) return; | |
146 | done = true; | |
147 | reject(self, ex); | |
148 | } | |
149 | } | |
150 | ||
151 | Promise.prototype['catch'] = function (onRejected) { | |
152 | return this.then(null, onRejected); | |
153 | }; | |
154 | ||
155 | Promise.prototype.then = function (onFulfilled, onRejected) { | |
156 | var prom = new (this.constructor)(noop); | |
157 | ||
158 | handle(this, new Handler(onFulfilled, onRejected, prom)); | |
159 | return prom; | |
160 | }; | |
161 | ||
162 | Promise.all = function (arr) { | |
163 | var args = Array.prototype.slice.call(arr); | |
164 | ||
165 | return new Promise(function (resolve, reject) { | |
166 | if (args.length === 0) return resolve([]); | |
167 | var remaining = args.length; | |
168 | ||
169 | function res(i, val) { | |
170 | try { | |
171 | if (val && (typeof val === 'object' || typeof val === 'function')) { | |
172 | var then = val.then; | |
173 | if (typeof then === 'function') { | |
174 | then.call(val, function (val) { | |
175 | res(i, val); | |
176 | }, reject); | |
177 | return; | |
178 | } | |
179 | } | |
180 | args[i] = val; | |
181 | if (--remaining === 0) { | |
182 | resolve(args); | |
183 | } | |
184 | } catch (ex) { | |
185 | reject(ex); | |
186 | } | |
187 | } | |
188 | ||
189 | for (var i = 0; i < args.length; i++) { | |
190 | res(i, args[i]); | |
191 | } | |
192 | }); | |
193 | }; | |
194 | ||
195 | Promise.resolve = function (value) { | |
196 | if (value && typeof value === 'object' && value.constructor === Promise) { | |
197 | return value; | |
198 | } | |
199 | ||
200 | return new Promise(function (resolve) { | |
201 | resolve(value); | |
202 | }); | |
203 | }; | |
204 | ||
205 | Promise.reject = function (value) { | |
206 | return new Promise(function (resolve, reject) { | |
207 | reject(value); | |
208 | }); | |
209 | }; | |
210 | ||
211 | Promise.race = function (values) { | |
212 | return new Promise(function (resolve, reject) { | |
213 | for (var i = 0, len = values.length; i < len; i++) { | |
214 | values[i].then(resolve, reject); | |
215 | } | |
216 | }); | |
217 | }; | |
218 | ||
219 | // Use polyfill for setImmediate for performance gains | |
220 | Promise._immediateFn = (typeof setImmediate === 'function' && function (fn) { setImmediate(fn); }) || | |
221 | function (fn) { | |
222 | setTimeoutFunc(fn, 0); | |
223 | }; | |
224 | ||
225 | Promise._unhandledRejectionFn = function _unhandledRejectionFn(err) { | |
226 | if (typeof console !== 'undefined' && console) { | |
227 | console.warn('Possible Unhandled Promise Rejection:', err); // eslint-disable-line no-console | |
228 | } | |
229 | }; | |
230 | ||
231 | /** | |
232 | * Set the immediate function to execute callbacks | |
233 | * @param fn {function} Function to execute | |
234 | * @deprecated | |
235 | */ | |
236 | Promise._setImmediateFn = function _setImmediateFn(fn) { | |
237 | Promise._immediateFn = fn; | |
238 | }; | |
239 | ||
240 | /** | |
241 | * Change the function to execute on unhandled rejection | |
242 | * @param {function} fn Function to execute on unhandled rejection | |
243 | * @deprecated | |
244 | */ | |
245 | Promise._setUnhandledRejectionFn = function _setUnhandledRejectionFn(fn) { | |
246 | Promise._unhandledRejectionFn = fn; | |
247 | }; | |
248 | ||
249 | if (typeof module !== 'undefined' && module.exports) { | |
250 | module.exports = Promise; | |
251 | } else if (!root.Promise) { | |
252 | root.Promise = Promise; | |
253 | } | |
254 | ||
255 | })(this); |