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