UNPKG

32.3 kBJavaScriptView Raw
1/*!
2 * @overview es6-promise - a tiny implementation of Promises/A+.
3 * @copyright Copyright (c) 2014 Yehuda Katz, Tom Dale, Stefan Penner and contributors (Conversion to ES6 API by Jake Archibald)
4 * @license Licensed under MIT license
5 * See https://raw.githubusercontent.com/jakearchibald/es6-promise/master/LICENSE
6 * @version 3.0.2
7 */
8
9(function() {
10 "use strict";
11 function lib$es6$promise$utils$$objectOrFunction(x) {
12 return typeof x === 'function' || (typeof x === 'object' && x !== null);
13 }
14
15 function lib$es6$promise$utils$$isFunction(x) {
16 return typeof x === 'function';
17 }
18
19 function lib$es6$promise$utils$$isMaybeThenable(x) {
20 return typeof x === 'object' && x !== null;
21 }
22
23 var lib$es6$promise$utils$$_isArray;
24 if (!Array.isArray) {
25 lib$es6$promise$utils$$_isArray = function (x) {
26 return Object.prototype.toString.call(x) === '[object Array]';
27 };
28 } else {
29 lib$es6$promise$utils$$_isArray = Array.isArray;
30 }
31
32 var lib$es6$promise$utils$$isArray = lib$es6$promise$utils$$_isArray;
33 var lib$es6$promise$asap$$len = 0;
34 var lib$es6$promise$asap$$toString = {}.toString;
35 var lib$es6$promise$asap$$vertxNext;
36 var lib$es6$promise$asap$$customSchedulerFn;
37
38 var lib$es6$promise$asap$$asap = function asap(callback, arg) {
39 lib$es6$promise$asap$$queue[lib$es6$promise$asap$$len] = callback;
40 lib$es6$promise$asap$$queue[lib$es6$promise$asap$$len + 1] = arg;
41 lib$es6$promise$asap$$len += 2;
42 if (lib$es6$promise$asap$$len === 2) {
43 // If len is 2, that means that we need to schedule an async flush.
44 // If additional callbacks are queued before the queue is flushed, they
45 // will be processed by this flush that we are scheduling.
46 if (lib$es6$promise$asap$$customSchedulerFn) {
47 lib$es6$promise$asap$$customSchedulerFn(lib$es6$promise$asap$$flush);
48 } else {
49 lib$es6$promise$asap$$scheduleFlush();
50 }
51 }
52 }
53
54 function lib$es6$promise$asap$$setScheduler(scheduleFn) {
55 lib$es6$promise$asap$$customSchedulerFn = scheduleFn;
56 }
57
58 function lib$es6$promise$asap$$setAsap(asapFn) {
59 lib$es6$promise$asap$$asap = asapFn;
60 }
61
62 var lib$es6$promise$asap$$browserWindow = (typeof window !== 'undefined') ? window : undefined;
63 var lib$es6$promise$asap$$browserGlobal = lib$es6$promise$asap$$browserWindow || {};
64 var lib$es6$promise$asap$$BrowserMutationObserver = lib$es6$promise$asap$$browserGlobal.MutationObserver || lib$es6$promise$asap$$browserGlobal.WebKitMutationObserver;
65 var lib$es6$promise$asap$$isNode = typeof process !== 'undefined' && {}.toString.call(process) === '[object process]';
66
67 // test for web worker but not in IE10
68 var lib$es6$promise$asap$$isWorker = typeof Uint8ClampedArray !== 'undefined' &&
69 typeof importScripts !== 'undefined' &&
70 typeof MessageChannel !== 'undefined';
71
72 // node
73 function lib$es6$promise$asap$$useNextTick() {
74 // node version 0.10.x displays a deprecation warning when nextTick is used recursively
75 // see https://github.com/cujojs/when/issues/410 for details
76 return function() {
77 process.nextTick(lib$es6$promise$asap$$flush);
78 };
79 }
80
81 // vertx
82 function lib$es6$promise$asap$$useVertxTimer() {
83 return function() {
84 lib$es6$promise$asap$$vertxNext(lib$es6$promise$asap$$flush);
85 };
86 }
87
88 function lib$es6$promise$asap$$useMutationObserver() {
89 var iterations = 0;
90 var observer = new lib$es6$promise$asap$$BrowserMutationObserver(lib$es6$promise$asap$$flush);
91 var node = document.createTextNode('');
92 observer.observe(node, { characterData: true });
93
94 return function() {
95 node.data = (iterations = ++iterations % 2);
96 };
97 }
98
99 // web worker
100 function lib$es6$promise$asap$$useMessageChannel() {
101 var channel = new MessageChannel();
102 channel.port1.onmessage = lib$es6$promise$asap$$flush;
103 return function () {
104 channel.port2.postMessage(0);
105 };
106 }
107
108 function lib$es6$promise$asap$$useSetTimeout() {
109 return function() {
110 setTimeout(lib$es6$promise$asap$$flush, 1);
111 };
112 }
113
114 var lib$es6$promise$asap$$queue = new Array(1000);
115 function lib$es6$promise$asap$$flush() {
116 for (var i = 0; i < lib$es6$promise$asap$$len; i+=2) {
117 var callback = lib$es6$promise$asap$$queue[i];
118 var arg = lib$es6$promise$asap$$queue[i+1];
119
120 callback(arg);
121
122 lib$es6$promise$asap$$queue[i] = undefined;
123 lib$es6$promise$asap$$queue[i+1] = undefined;
124 }
125
126 lib$es6$promise$asap$$len = 0;
127 }
128
129 function lib$es6$promise$asap$$attemptVertx() {
130 try {
131 var r = require;
132 var vertx = r('vertx');
133 lib$es6$promise$asap$$vertxNext = vertx.runOnLoop || vertx.runOnContext;
134 return lib$es6$promise$asap$$useVertxTimer();
135 } catch(e) {
136 return lib$es6$promise$asap$$useSetTimeout();
137 }
138 }
139
140 var lib$es6$promise$asap$$scheduleFlush;
141 // Decide what async method to use to triggering processing of queued callbacks:
142 if (lib$es6$promise$asap$$isNode) {
143 lib$es6$promise$asap$$scheduleFlush = lib$es6$promise$asap$$useNextTick();
144 } else if (lib$es6$promise$asap$$BrowserMutationObserver) {
145 lib$es6$promise$asap$$scheduleFlush = lib$es6$promise$asap$$useMutationObserver();
146 } else if (lib$es6$promise$asap$$isWorker) {
147 lib$es6$promise$asap$$scheduleFlush = lib$es6$promise$asap$$useMessageChannel();
148 } else if (lib$es6$promise$asap$$browserWindow === undefined && typeof require === 'function') {
149 lib$es6$promise$asap$$scheduleFlush = lib$es6$promise$asap$$attemptVertx();
150 } else {
151 lib$es6$promise$asap$$scheduleFlush = lib$es6$promise$asap$$useSetTimeout();
152 }
153
154 function lib$es6$promise$$internal$$noop() {}
155
156 var lib$es6$promise$$internal$$PENDING = void 0;
157 var lib$es6$promise$$internal$$FULFILLED = 1;
158 var lib$es6$promise$$internal$$REJECTED = 2;
159
160 var lib$es6$promise$$internal$$GET_THEN_ERROR = new lib$es6$promise$$internal$$ErrorObject();
161
162 function lib$es6$promise$$internal$$selfFulfillment() {
163 return new TypeError("You cannot resolve a promise with itself");
164 }
165
166 function lib$es6$promise$$internal$$cannotReturnOwn() {
167 return new TypeError('A promises callback cannot return that same promise.');
168 }
169
170 function lib$es6$promise$$internal$$getThen(promise) {
171 try {
172 return promise.then;
173 } catch(error) {
174 lib$es6$promise$$internal$$GET_THEN_ERROR.error = error;
175 return lib$es6$promise$$internal$$GET_THEN_ERROR;
176 }
177 }
178
179 function lib$es6$promise$$internal$$tryThen(then, value, fulfillmentHandler, rejectionHandler) {
180 try {
181 then.call(value, fulfillmentHandler, rejectionHandler);
182 } catch(e) {
183 return e;
184 }
185 }
186
187 function lib$es6$promise$$internal$$handleForeignThenable(promise, thenable, then) {
188 lib$es6$promise$asap$$asap(function(promise) {
189 var sealed = false;
190 var error = lib$es6$promise$$internal$$tryThen(then, thenable, function(value) {
191 if (sealed) { return; }
192 sealed = true;
193 if (thenable !== value) {
194 lib$es6$promise$$internal$$resolve(promise, value);
195 } else {
196 lib$es6$promise$$internal$$fulfill(promise, value);
197 }
198 }, function(reason) {
199 if (sealed) { return; }
200 sealed = true;
201
202 lib$es6$promise$$internal$$reject(promise, reason);
203 }, 'Settle: ' + (promise._label || ' unknown promise'));
204
205 if (!sealed && error) {
206 sealed = true;
207 lib$es6$promise$$internal$$reject(promise, error);
208 }
209 }, promise);
210 }
211
212 function lib$es6$promise$$internal$$handleOwnThenable(promise, thenable) {
213 if (thenable._state === lib$es6$promise$$internal$$FULFILLED) {
214 lib$es6$promise$$internal$$fulfill(promise, thenable._result);
215 } else if (thenable._state === lib$es6$promise$$internal$$REJECTED) {
216 lib$es6$promise$$internal$$reject(promise, thenable._result);
217 } else {
218 lib$es6$promise$$internal$$subscribe(thenable, undefined, function(value) {
219 lib$es6$promise$$internal$$resolve(promise, value);
220 }, function(reason) {
221 lib$es6$promise$$internal$$reject(promise, reason);
222 });
223 }
224 }
225
226 function lib$es6$promise$$internal$$handleMaybeThenable(promise, maybeThenable) {
227 if (maybeThenable.constructor === promise.constructor) {
228 lib$es6$promise$$internal$$handleOwnThenable(promise, maybeThenable);
229 } else {
230 var then = lib$es6$promise$$internal$$getThen(maybeThenable);
231
232 if (then === lib$es6$promise$$internal$$GET_THEN_ERROR) {
233 lib$es6$promise$$internal$$reject(promise, lib$es6$promise$$internal$$GET_THEN_ERROR.error);
234 } else if (then === undefined) {
235 lib$es6$promise$$internal$$fulfill(promise, maybeThenable);
236 } else if (lib$es6$promise$utils$$isFunction(then)) {
237 lib$es6$promise$$internal$$handleForeignThenable(promise, maybeThenable, then);
238 } else {
239 lib$es6$promise$$internal$$fulfill(promise, maybeThenable);
240 }
241 }
242 }
243
244 function lib$es6$promise$$internal$$resolve(promise, value) {
245 if (promise === value) {
246 lib$es6$promise$$internal$$reject(promise, lib$es6$promise$$internal$$selfFulfillment());
247 } else if (lib$es6$promise$utils$$objectOrFunction(value)) {
248 lib$es6$promise$$internal$$handleMaybeThenable(promise, value);
249 } else {
250 lib$es6$promise$$internal$$fulfill(promise, value);
251 }
252 }
253
254 function lib$es6$promise$$internal$$publishRejection(promise) {
255 if (promise._onerror) {
256 promise._onerror(promise._result);
257 }
258
259 lib$es6$promise$$internal$$publish(promise);
260 }
261
262 function lib$es6$promise$$internal$$fulfill(promise, value) {
263 if (promise._state !== lib$es6$promise$$internal$$PENDING) { return; }
264
265 promise._result = value;
266 promise._state = lib$es6$promise$$internal$$FULFILLED;
267
268 if (promise._subscribers.length !== 0) {
269 lib$es6$promise$asap$$asap(lib$es6$promise$$internal$$publish, promise);
270 }
271 }
272
273 function lib$es6$promise$$internal$$reject(promise, reason) {
274 if (promise._state !== lib$es6$promise$$internal$$PENDING) { return; }
275 promise._state = lib$es6$promise$$internal$$REJECTED;
276 promise._result = reason;
277
278 lib$es6$promise$asap$$asap(lib$es6$promise$$internal$$publishRejection, promise);
279 }
280
281 function lib$es6$promise$$internal$$subscribe(parent, child, onFulfillment, onRejection) {
282 var subscribers = parent._subscribers;
283 var length = subscribers.length;
284
285 parent._onerror = null;
286
287 subscribers[length] = child;
288 subscribers[length + lib$es6$promise$$internal$$FULFILLED] = onFulfillment;
289 subscribers[length + lib$es6$promise$$internal$$REJECTED] = onRejection;
290
291 if (length === 0 && parent._state) {
292 lib$es6$promise$asap$$asap(lib$es6$promise$$internal$$publish, parent);
293 }
294 }
295
296 function lib$es6$promise$$internal$$publish(promise) {
297 var subscribers = promise._subscribers;
298 var settled = promise._state;
299
300 if (subscribers.length === 0) { return; }
301
302 var child, callback, detail = promise._result;
303
304 for (var i = 0; i < subscribers.length; i += 3) {
305 child = subscribers[i];
306 callback = subscribers[i + settled];
307
308 if (child) {
309 lib$es6$promise$$internal$$invokeCallback(settled, child, callback, detail);
310 } else {
311 callback(detail);
312 }
313 }
314
315 promise._subscribers.length = 0;
316 }
317
318 function lib$es6$promise$$internal$$ErrorObject() {
319 this.error = null;
320 }
321
322 var lib$es6$promise$$internal$$TRY_CATCH_ERROR = new lib$es6$promise$$internal$$ErrorObject();
323
324 function lib$es6$promise$$internal$$tryCatch(callback, detail) {
325 try {
326 return callback(detail);
327 } catch(e) {
328 lib$es6$promise$$internal$$TRY_CATCH_ERROR.error = e;
329 return lib$es6$promise$$internal$$TRY_CATCH_ERROR;
330 }
331 }
332
333 function lib$es6$promise$$internal$$invokeCallback(settled, promise, callback, detail) {
334 var hasCallback = lib$es6$promise$utils$$isFunction(callback),
335 value, error, succeeded, failed;
336
337 if (hasCallback) {
338 value = lib$es6$promise$$internal$$tryCatch(callback, detail);
339
340 if (value === lib$es6$promise$$internal$$TRY_CATCH_ERROR) {
341 failed = true;
342 error = value.error;
343 value = null;
344 } else {
345 succeeded = true;
346 }
347
348 if (promise === value) {
349 lib$es6$promise$$internal$$reject(promise, lib$es6$promise$$internal$$cannotReturnOwn());
350 return;
351 }
352
353 } else {
354 value = detail;
355 succeeded = true;
356 }
357
358 if (promise._state !== lib$es6$promise$$internal$$PENDING) {
359 // noop
360 } else if (hasCallback && succeeded) {
361 lib$es6$promise$$internal$$resolve(promise, value);
362 } else if (failed) {
363 lib$es6$promise$$internal$$reject(promise, error);
364 } else if (settled === lib$es6$promise$$internal$$FULFILLED) {
365 lib$es6$promise$$internal$$fulfill(promise, value);
366 } else if (settled === lib$es6$promise$$internal$$REJECTED) {
367 lib$es6$promise$$internal$$reject(promise, value);
368 }
369 }
370
371 function lib$es6$promise$$internal$$initializePromise(promise, resolver) {
372 try {
373 resolver(function resolvePromise(value){
374 lib$es6$promise$$internal$$resolve(promise, value);
375 }, function rejectPromise(reason) {
376 lib$es6$promise$$internal$$reject(promise, reason);
377 });
378 } catch(e) {
379 lib$es6$promise$$internal$$reject(promise, e);
380 }
381 }
382
383 function lib$es6$promise$enumerator$$Enumerator(Constructor, input) {
384 var enumerator = this;
385
386 enumerator._instanceConstructor = Constructor;
387 enumerator.promise = new Constructor(lib$es6$promise$$internal$$noop);
388
389 if (enumerator._validateInput(input)) {
390 enumerator._input = input;
391 enumerator.length = input.length;
392 enumerator._remaining = input.length;
393
394 enumerator._init();
395
396 if (enumerator.length === 0) {
397 lib$es6$promise$$internal$$fulfill(enumerator.promise, enumerator._result);
398 } else {
399 enumerator.length = enumerator.length || 0;
400 enumerator._enumerate();
401 if (enumerator._remaining === 0) {
402 lib$es6$promise$$internal$$fulfill(enumerator.promise, enumerator._result);
403 }
404 }
405 } else {
406 lib$es6$promise$$internal$$reject(enumerator.promise, enumerator._validationError());
407 }
408 }
409
410 lib$es6$promise$enumerator$$Enumerator.prototype._validateInput = function(input) {
411 return lib$es6$promise$utils$$isArray(input);
412 };
413
414 lib$es6$promise$enumerator$$Enumerator.prototype._validationError = function() {
415 return new Error('Array Methods must be provided an Array');
416 };
417
418 lib$es6$promise$enumerator$$Enumerator.prototype._init = function() {
419 this._result = new Array(this.length);
420 };
421
422 var lib$es6$promise$enumerator$$default = lib$es6$promise$enumerator$$Enumerator;
423
424 lib$es6$promise$enumerator$$Enumerator.prototype._enumerate = function() {
425 var enumerator = this;
426
427 var length = enumerator.length;
428 var promise = enumerator.promise;
429 var input = enumerator._input;
430
431 for (var i = 0; promise._state === lib$es6$promise$$internal$$PENDING && i < length; i++) {
432 enumerator._eachEntry(input[i], i);
433 }
434 };
435
436 lib$es6$promise$enumerator$$Enumerator.prototype._eachEntry = function(entry, i) {
437 var enumerator = this;
438 var c = enumerator._instanceConstructor;
439
440 if (lib$es6$promise$utils$$isMaybeThenable(entry)) {
441 if (entry.constructor === c && entry._state !== lib$es6$promise$$internal$$PENDING) {
442 entry._onerror = null;
443 enumerator._settledAt(entry._state, i, entry._result);
444 } else {
445 enumerator._willSettleAt(c.resolve(entry), i);
446 }
447 } else {
448 enumerator._remaining--;
449 enumerator._result[i] = entry;
450 }
451 };
452
453 lib$es6$promise$enumerator$$Enumerator.prototype._settledAt = function(state, i, value) {
454 var enumerator = this;
455 var promise = enumerator.promise;
456
457 if (promise._state === lib$es6$promise$$internal$$PENDING) {
458 enumerator._remaining--;
459
460 if (state === lib$es6$promise$$internal$$REJECTED) {
461 lib$es6$promise$$internal$$reject(promise, value);
462 } else {
463 enumerator._result[i] = value;
464 }
465 }
466
467 if (enumerator._remaining === 0) {
468 lib$es6$promise$$internal$$fulfill(promise, enumerator._result);
469 }
470 };
471
472 lib$es6$promise$enumerator$$Enumerator.prototype._willSettleAt = function(promise, i) {
473 var enumerator = this;
474
475 lib$es6$promise$$internal$$subscribe(promise, undefined, function(value) {
476 enumerator._settledAt(lib$es6$promise$$internal$$FULFILLED, i, value);
477 }, function(reason) {
478 enumerator._settledAt(lib$es6$promise$$internal$$REJECTED, i, reason);
479 });
480 };
481 function lib$es6$promise$promise$all$$all(entries) {
482 return new lib$es6$promise$enumerator$$default(this, entries).promise;
483 }
484 var lib$es6$promise$promise$all$$default = lib$es6$promise$promise$all$$all;
485 function lib$es6$promise$promise$race$$race(entries) {
486 /*jshint validthis:true */
487 var Constructor = this;
488
489 var promise = new Constructor(lib$es6$promise$$internal$$noop);
490
491 if (!lib$es6$promise$utils$$isArray(entries)) {
492 lib$es6$promise$$internal$$reject(promise, new TypeError('You must pass an array to race.'));
493 return promise;
494 }
495
496 var length = entries.length;
497
498 function onFulfillment(value) {
499 lib$es6$promise$$internal$$resolve(promise, value);
500 }
501
502 function onRejection(reason) {
503 lib$es6$promise$$internal$$reject(promise, reason);
504 }
505
506 for (var i = 0; promise._state === lib$es6$promise$$internal$$PENDING && i < length; i++) {
507 lib$es6$promise$$internal$$subscribe(Constructor.resolve(entries[i]), undefined, onFulfillment, onRejection);
508 }
509
510 return promise;
511 }
512 var lib$es6$promise$promise$race$$default = lib$es6$promise$promise$race$$race;
513 function lib$es6$promise$promise$resolve$$resolve(object) {
514 /*jshint validthis:true */
515 var Constructor = this;
516
517 if (object && typeof object === 'object' && object.constructor === Constructor) {
518 return object;
519 }
520
521 var promise = new Constructor(lib$es6$promise$$internal$$noop);
522 lib$es6$promise$$internal$$resolve(promise, object);
523 return promise;
524 }
525 var lib$es6$promise$promise$resolve$$default = lib$es6$promise$promise$resolve$$resolve;
526 function lib$es6$promise$promise$reject$$reject(reason) {
527 /*jshint validthis:true */
528 var Constructor = this;
529 var promise = new Constructor(lib$es6$promise$$internal$$noop);
530 lib$es6$promise$$internal$$reject(promise, reason);
531 return promise;
532 }
533 var lib$es6$promise$promise$reject$$default = lib$es6$promise$promise$reject$$reject;
534
535 var lib$es6$promise$promise$$counter = 0;
536
537 function lib$es6$promise$promise$$needsResolver() {
538 throw new TypeError('You must pass a resolver function as the first argument to the promise constructor');
539 }
540
541 function lib$es6$promise$promise$$needsNew() {
542 throw new TypeError("Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function.");
543 }
544
545 var lib$es6$promise$promise$$default = lib$es6$promise$promise$$Promise;
546 /**
547 Promise objects represent the eventual result of an asynchronous operation. The
548 primary way of interacting with a promise is through its `then` method, which
549 registers callbacks to receive either a promise's eventual value or the reason
550 why the promise cannot be fulfilled.
551
552 Terminology
553 -----------
554
555 - `promise` is an object or function with a `then` method whose behavior conforms to this specification.
556 - `thenable` is an object or function that defines a `then` method.
557 - `value` is any legal JavaScript value (including undefined, a thenable, or a promise).
558 - `exception` is a value that is thrown using the throw statement.
559 - `reason` is a value that indicates why a promise was rejected.
560 - `settled` the final resting state of a promise, fulfilled or rejected.
561
562 A promise can be in one of three states: pending, fulfilled, or rejected.
563
564 Promises that are fulfilled have a fulfillment value and are in the fulfilled
565 state. Promises that are rejected have a rejection reason and are in the
566 rejected state. A fulfillment value is never a thenable.
567
568 Promises can also be said to *resolve* a value. If this value is also a
569 promise, then the original promise's settled state will match the value's
570 settled state. So a promise that *resolves* a promise that rejects will
571 itself reject, and a promise that *resolves* a promise that fulfills will
572 itself fulfill.
573
574
575 Basic Usage:
576 ------------
577
578 ```js
579 var promise = new Promise(function(resolve, reject) {
580 // on success
581 resolve(value);
582
583 // on failure
584 reject(reason);
585 });
586
587 promise.then(function(value) {
588 // on fulfillment
589 }, function(reason) {
590 // on rejection
591 });
592 ```
593
594 Advanced Usage:
595 ---------------
596
597 Promises shine when abstracting away asynchronous interactions such as
598 `XMLHttpRequest`s.
599
600 ```js
601 function getJSON(url) {
602 return new Promise(function(resolve, reject){
603 var xhr = new XMLHttpRequest();
604
605 xhr.open('GET', url);
606 xhr.onreadystatechange = handler;
607 xhr.responseType = 'json';
608 xhr.setRequestHeader('Accept', 'application/json');
609 xhr.send();
610
611 function handler() {
612 if (this.readyState === this.DONE) {
613 if (this.status === 200) {
614 resolve(this.response);
615 } else {
616 reject(new Error('getJSON: `' + url + '` failed with status: [' + this.status + ']'));
617 }
618 }
619 };
620 });
621 }
622
623 getJSON('/posts.json').then(function(json) {
624 // on fulfillment
625 }, function(reason) {
626 // on rejection
627 });
628 ```
629
630 Unlike callbacks, promises are great composable primitives.
631
632 ```js
633 Promise.all([
634 getJSON('/posts'),
635 getJSON('/comments')
636 ]).then(function(values){
637 values[0] // => postsJSON
638 values[1] // => commentsJSON
639
640 return values;
641 });
642 ```
643
644 @class Promise
645 @param {function} resolver
646 Useful for tooling.
647 @constructor
648 */
649 function lib$es6$promise$promise$$Promise(resolver) {
650 this._id = lib$es6$promise$promise$$counter++;
651 this._state = undefined;
652 this._result = undefined;
653 this._subscribers = [];
654
655 if (lib$es6$promise$$internal$$noop !== resolver) {
656 if (!lib$es6$promise$utils$$isFunction(resolver)) {
657 lib$es6$promise$promise$$needsResolver();
658 }
659
660 if (!(this instanceof lib$es6$promise$promise$$Promise)) {
661 lib$es6$promise$promise$$needsNew();
662 }
663
664 lib$es6$promise$$internal$$initializePromise(this, resolver);
665 }
666 }
667
668 lib$es6$promise$promise$$Promise.all = lib$es6$promise$promise$all$$default;
669 lib$es6$promise$promise$$Promise.race = lib$es6$promise$promise$race$$default;
670 lib$es6$promise$promise$$Promise.resolve = lib$es6$promise$promise$resolve$$default;
671 lib$es6$promise$promise$$Promise.reject = lib$es6$promise$promise$reject$$default;
672 lib$es6$promise$promise$$Promise._setScheduler = lib$es6$promise$asap$$setScheduler;
673 lib$es6$promise$promise$$Promise._setAsap = lib$es6$promise$asap$$setAsap;
674 lib$es6$promise$promise$$Promise._asap = lib$es6$promise$asap$$asap;
675
676 lib$es6$promise$promise$$Promise.prototype = {
677 constructor: lib$es6$promise$promise$$Promise,
678
679 /**
680 The primary way of interacting with a promise is through its `then` method,
681 which registers callbacks to receive either a promise's eventual value or the
682 reason why the promise cannot be fulfilled.
683
684 ```js
685 findUser().then(function(user){
686 // user is available
687 }, function(reason){
688 // user is unavailable, and you are given the reason why
689 });
690 ```
691
692 Chaining
693 --------
694
695 The return value of `then` is itself a promise. This second, 'downstream'
696 promise is resolved with the return value of the first promise's fulfillment
697 or rejection handler, or rejected if the handler throws an exception.
698
699 ```js
700 findUser().then(function (user) {
701 return user.name;
702 }, function (reason) {
703 return 'default name';
704 }).then(function (userName) {
705 // If `findUser` fulfilled, `userName` will be the user's name, otherwise it
706 // will be `'default name'`
707 });
708
709 findUser().then(function (user) {
710 throw new Error('Found user, but still unhappy');
711 }, function (reason) {
712 throw new Error('`findUser` rejected and we're unhappy');
713 }).then(function (value) {
714 // never reached
715 }, function (reason) {
716 // if `findUser` fulfilled, `reason` will be 'Found user, but still unhappy'.
717 // If `findUser` rejected, `reason` will be '`findUser` rejected and we're unhappy'.
718 });
719 ```
720 If the downstream promise does not specify a rejection handler, rejection reasons will be propagated further downstream.
721
722 ```js
723 findUser().then(function (user) {
724 throw new PedagogicalException('Upstream error');
725 }).then(function (value) {
726 // never reached
727 }).then(function (value) {
728 // never reached
729 }, function (reason) {
730 // The `PedgagocialException` is propagated all the way down to here
731 });
732 ```
733
734 Assimilation
735 ------------
736
737 Sometimes the value you want to propagate to a downstream promise can only be
738 retrieved asynchronously. This can be achieved by returning a promise in the
739 fulfillment or rejection handler. The downstream promise will then be pending
740 until the returned promise is settled. This is called *assimilation*.
741
742 ```js
743 findUser().then(function (user) {
744 return findCommentsByAuthor(user);
745 }).then(function (comments) {
746 // The user's comments are now available
747 });
748 ```
749
750 If the assimliated promise rejects, then the downstream promise will also reject.
751
752 ```js
753 findUser().then(function (user) {
754 return findCommentsByAuthor(user);
755 }).then(function (comments) {
756 // If `findCommentsByAuthor` fulfills, we'll have the value here
757 }, function (reason) {
758 // If `findCommentsByAuthor` rejects, we'll have the reason here
759 });
760 ```
761
762 Simple Example
763 --------------
764
765 Synchronous Example
766
767 ```javascript
768 var result;
769
770 try {
771 result = findResult();
772 // success
773 } catch(reason) {
774 // failure
775 }
776 ```
777
778 Errback Example
779
780 ```js
781 findResult(function(result, err){
782 if (err) {
783 // failure
784 } else {
785 // success
786 }
787 });
788 ```
789
790 Promise Example;
791
792 ```javascript
793 findResult().then(function(result){
794 // success
795 }, function(reason){
796 // failure
797 });
798 ```
799
800 Advanced Example
801 --------------
802
803 Synchronous Example
804
805 ```javascript
806 var author, books;
807
808 try {
809 author = findAuthor();
810 books = findBooksByAuthor(author);
811 // success
812 } catch(reason) {
813 // failure
814 }
815 ```
816
817 Errback Example
818
819 ```js
820
821 function foundBooks(books) {
822
823 }
824
825 function failure(reason) {
826
827 }
828
829 findAuthor(function(author, err){
830 if (err) {
831 failure(err);
832 // failure
833 } else {
834 try {
835 findBoooksByAuthor(author, function(books, err) {
836 if (err) {
837 failure(err);
838 } else {
839 try {
840 foundBooks(books);
841 } catch(reason) {
842 failure(reason);
843 }
844 }
845 });
846 } catch(error) {
847 failure(err);
848 }
849 // success
850 }
851 });
852 ```
853
854 Promise Example;
855
856 ```javascript
857 findAuthor().
858 then(findBooksByAuthor).
859 then(function(books){
860 // found books
861 }).catch(function(reason){
862 // something went wrong
863 });
864 ```
865
866 @method then
867 @param {Function} onFulfilled
868 @param {Function} onRejected
869 Useful for tooling.
870 @return {Promise}
871 */
872 then: function(onFulfillment, onRejection) {
873 var parent = this;
874 var state = parent._state;
875
876 if (state === lib$es6$promise$$internal$$FULFILLED && !onFulfillment || state === lib$es6$promise$$internal$$REJECTED && !onRejection) {
877 return this;
878 }
879
880 var child = new this.constructor(lib$es6$promise$$internal$$noop);
881 var result = parent._result;
882
883 if (state) {
884 var callback = arguments[state - 1];
885 lib$es6$promise$asap$$asap(function(){
886 lib$es6$promise$$internal$$invokeCallback(state, child, callback, result);
887 });
888 } else {
889 lib$es6$promise$$internal$$subscribe(parent, child, onFulfillment, onRejection);
890 }
891
892 return child;
893 },
894
895 /**
896 `catch` is simply sugar for `then(undefined, onRejection)` which makes it the same
897 as the catch block of a try/catch statement.
898
899 ```js
900 function findAuthor(){
901 throw new Error('couldn't find that author');
902 }
903
904 // synchronous
905 try {
906 findAuthor();
907 } catch(reason) {
908 // something went wrong
909 }
910
911 // async with promises
912 findAuthor().catch(function(reason){
913 // something went wrong
914 });
915 ```
916
917 @method catch
918 @param {Function} onRejection
919 Useful for tooling.
920 @return {Promise}
921 */
922 'catch': function(onRejection) {
923 return this.then(null, onRejection);
924 }
925 };
926 function lib$es6$promise$polyfill$$polyfill() {
927 var local;
928
929 if (typeof global !== 'undefined') {
930 local = global;
931 } else if (typeof self !== 'undefined') {
932 local = self;
933 } else {
934 try {
935 local = Function('return this')();
936 } catch (e) {
937 throw new Error('polyfill failed because global object is unavailable in this environment');
938 }
939 }
940
941 var P = local.Promise;
942
943 if (P && Object.prototype.toString.call(P.resolve()) === '[object Promise]' && !P.cast) {
944 return;
945 }
946
947 local.Promise = lib$es6$promise$promise$$default;
948 }
949 var lib$es6$promise$polyfill$$default = lib$es6$promise$polyfill$$polyfill;
950
951 var lib$es6$promise$umd$$ES6Promise = {
952 'Promise': lib$es6$promise$promise$$default,
953 'polyfill': lib$es6$promise$polyfill$$default
954 };
955
956 /* global define:true module:true window: true */
957 if (typeof define === 'function' && define['amd']) {
958 define(function() { return lib$es6$promise$umd$$ES6Promise; });
959 } else if (typeof module !== 'undefined' && module['exports']) {
960 module['exports'] = lib$es6$promise$umd$$ES6Promise;
961 } else if (typeof this !== 'undefined') {
962 this['ES6Promise'] = lib$es6$promise$umd$$ES6Promise;
963 }
964
965 lib$es6$promise$polyfill$$default();
966}).call(this);
967
968
\No newline at end of file