Nav apraksta

es6-promise.js 32KB

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