Açıklama Yok

es6-promise.js 31KB

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