es6.promise.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. 'use strict';
  2. var LIBRARY = require('./_library');
  3. var global = require('./_global');
  4. var ctx = require('./_ctx');
  5. var classof = require('./_classof');
  6. var $export = require('./_export');
  7. var isObject = require('./_is-object');
  8. var aFunction = require('./_a-function');
  9. var anInstance = require('./_an-instance');
  10. var forOf = require('./_for-of');
  11. var speciesConstructor = require('./_species-constructor');
  12. var task = require('./_task').set;
  13. var microtask = require('./_microtask')();
  14. var newPromiseCapabilityModule = require('./_new-promise-capability');
  15. var perform = require('./_perform');
  16. var userAgent = require('./_user-agent');
  17. var promiseResolve = require('./_promise-resolve');
  18. var PROMISE = 'Promise';
  19. var TypeError = global.TypeError;
  20. var process = global.process;
  21. var versions = process && process.versions;
  22. var v8 = versions && versions.v8 || '';
  23. var $Promise = global[PROMISE];
  24. var isNode = classof(process) == 'process';
  25. var empty = function () { /* empty */ };
  26. var Internal, newGenericPromiseCapability, OwnPromiseCapability, Wrapper;
  27. var newPromiseCapability = newGenericPromiseCapability = newPromiseCapabilityModule.f;
  28. var USE_NATIVE = !!function () {
  29. try {
  30. // correct subclassing with @@species support
  31. var promise = $Promise.resolve(1);
  32. var FakePromise = (promise.constructor = {})[require('./_wks')('species')] = function (exec) {
  33. exec(empty, empty);
  34. };
  35. // unhandled rejections tracking support, NodeJS Promise without it fails @@species test
  36. return (isNode || typeof PromiseRejectionEvent == 'function')
  37. && promise.then(empty) instanceof FakePromise
  38. // v8 6.6 (Node 10 and Chrome 66) have a bug with resolving custom thenables
  39. // https://bugs.chromium.org/p/chromium/issues/detail?id=830565
  40. // we can't detect it synchronously, so just check versions
  41. && v8.indexOf('6.6') !== 0
  42. && userAgent.indexOf('Chrome/66') === -1;
  43. } catch (e) { /* empty */ }
  44. }();
  45. // helpers
  46. var isThenable = function (it) {
  47. var then;
  48. return isObject(it) && typeof (then = it.then) == 'function' ? then : false;
  49. };
  50. var notify = function (promise, isReject) {
  51. if (promise._n) return;
  52. promise._n = true;
  53. var chain = promise._c;
  54. microtask(function () {
  55. var value = promise._v;
  56. var ok = promise._s == 1;
  57. var i = 0;
  58. var run = function (reaction) {
  59. var handler = ok ? reaction.ok : reaction.fail;
  60. var resolve = reaction.resolve;
  61. var reject = reaction.reject;
  62. var domain = reaction.domain;
  63. var result, then, exited;
  64. try {
  65. if (handler) {
  66. if (!ok) {
  67. if (promise._h == 2) onHandleUnhandled(promise);
  68. promise._h = 1;
  69. }
  70. if (handler === true) result = value;
  71. else {
  72. if (domain) domain.enter();
  73. result = handler(value); // may throw
  74. if (domain) {
  75. domain.exit();
  76. exited = true;
  77. }
  78. }
  79. if (result === reaction.promise) {
  80. reject(TypeError('Promise-chain cycle'));
  81. } else if (then = isThenable(result)) {
  82. then.call(result, resolve, reject);
  83. } else resolve(result);
  84. } else reject(value);
  85. } catch (e) {
  86. if (domain && !exited) domain.exit();
  87. reject(e);
  88. }
  89. };
  90. while (chain.length > i) run(chain[i++]); // variable length - can't use forEach
  91. promise._c = [];
  92. promise._n = false;
  93. if (isReject && !promise._h) onUnhandled(promise);
  94. });
  95. };
  96. var onUnhandled = function (promise) {
  97. task.call(global, function () {
  98. var value = promise._v;
  99. var unhandled = isUnhandled(promise);
  100. var result, handler, console;
  101. if (unhandled) {
  102. result = perform(function () {
  103. if (isNode) {
  104. process.emit('unhandledRejection', value, promise);
  105. } else if (handler = global.onunhandledrejection) {
  106. handler({ promise: promise, reason: value });
  107. } else if ((console = global.console) && console.error) {
  108. console.error('Unhandled promise rejection', value);
  109. }
  110. });
  111. // Browsers should not trigger `rejectionHandled` event if it was handled here, NodeJS - should
  112. promise._h = isNode || isUnhandled(promise) ? 2 : 1;
  113. } promise._a = undefined;
  114. if (unhandled && result.e) throw result.v;
  115. });
  116. };
  117. var isUnhandled = function (promise) {
  118. return promise._h !== 1 && (promise._a || promise._c).length === 0;
  119. };
  120. var onHandleUnhandled = function (promise) {
  121. task.call(global, function () {
  122. var handler;
  123. if (isNode) {
  124. process.emit('rejectionHandled', promise);
  125. } else if (handler = global.onrejectionhandled) {
  126. handler({ promise: promise, reason: promise._v });
  127. }
  128. });
  129. };
  130. var $reject = function (value) {
  131. var promise = this;
  132. if (promise._d) return;
  133. promise._d = true;
  134. promise = promise._w || promise; // unwrap
  135. promise._v = value;
  136. promise._s = 2;
  137. if (!promise._a) promise._a = promise._c.slice();
  138. notify(promise, true);
  139. };
  140. var $resolve = function (value) {
  141. var promise = this;
  142. var then;
  143. if (promise._d) return;
  144. promise._d = true;
  145. promise = promise._w || promise; // unwrap
  146. try {
  147. if (promise === value) throw TypeError("Promise can't be resolved itself");
  148. if (then = isThenable(value)) {
  149. microtask(function () {
  150. var wrapper = { _w: promise, _d: false }; // wrap
  151. try {
  152. then.call(value, ctx($resolve, wrapper, 1), ctx($reject, wrapper, 1));
  153. } catch (e) {
  154. $reject.call(wrapper, e);
  155. }
  156. });
  157. } else {
  158. promise._v = value;
  159. promise._s = 1;
  160. notify(promise, false);
  161. }
  162. } catch (e) {
  163. $reject.call({ _w: promise, _d: false }, e); // wrap
  164. }
  165. };
  166. // constructor polyfill
  167. if (!USE_NATIVE) {
  168. // 25.4.3.1 Promise(executor)
  169. $Promise = function Promise(executor) {
  170. anInstance(this, $Promise, PROMISE, '_h');
  171. aFunction(executor);
  172. Internal.call(this);
  173. try {
  174. executor(ctx($resolve, this, 1), ctx($reject, this, 1));
  175. } catch (err) {
  176. $reject.call(this, err);
  177. }
  178. };
  179. // eslint-disable-next-line no-unused-vars
  180. Internal = function Promise(executor) {
  181. this._c = []; // <- awaiting reactions
  182. this._a = undefined; // <- checked in isUnhandled reactions
  183. this._s = 0; // <- state
  184. this._d = false; // <- done
  185. this._v = undefined; // <- value
  186. this._h = 0; // <- rejection state, 0 - default, 1 - handled, 2 - unhandled
  187. this._n = false; // <- notify
  188. };
  189. Internal.prototype = require('./_redefine-all')($Promise.prototype, {
  190. // 25.4.5.3 Promise.prototype.then(onFulfilled, onRejected)
  191. then: function then(onFulfilled, onRejected) {
  192. var reaction = newPromiseCapability(speciesConstructor(this, $Promise));
  193. reaction.ok = typeof onFulfilled == 'function' ? onFulfilled : true;
  194. reaction.fail = typeof onRejected == 'function' && onRejected;
  195. reaction.domain = isNode ? process.domain : undefined;
  196. this._c.push(reaction);
  197. if (this._a) this._a.push(reaction);
  198. if (this._s) notify(this, false);
  199. return reaction.promise;
  200. },
  201. // 25.4.5.1 Promise.prototype.catch(onRejected)
  202. 'catch': function (onRejected) {
  203. return this.then(undefined, onRejected);
  204. }
  205. });
  206. OwnPromiseCapability = function () {
  207. var promise = new Internal();
  208. this.promise = promise;
  209. this.resolve = ctx($resolve, promise, 1);
  210. this.reject = ctx($reject, promise, 1);
  211. };
  212. newPromiseCapabilityModule.f = newPromiseCapability = function (C) {
  213. return C === $Promise || C === Wrapper
  214. ? new OwnPromiseCapability(C)
  215. : newGenericPromiseCapability(C);
  216. };
  217. }
  218. $export($export.G + $export.W + $export.F * !USE_NATIVE, { Promise: $Promise });
  219. require('./_set-to-string-tag')($Promise, PROMISE);
  220. require('./_set-species')(PROMISE);
  221. Wrapper = require('./_core')[PROMISE];
  222. // statics
  223. $export($export.S + $export.F * !USE_NATIVE, PROMISE, {
  224. // 25.4.4.5 Promise.reject(r)
  225. reject: function reject(r) {
  226. var capability = newPromiseCapability(this);
  227. var $$reject = capability.reject;
  228. $$reject(r);
  229. return capability.promise;
  230. }
  231. });
  232. $export($export.S + $export.F * (LIBRARY || !USE_NATIVE), PROMISE, {
  233. // 25.4.4.6 Promise.resolve(x)
  234. resolve: function resolve(x) {
  235. return promiseResolve(LIBRARY && this === Wrapper ? $Promise : this, x);
  236. }
  237. });
  238. $export($export.S + $export.F * !(USE_NATIVE && require('./_iter-detect')(function (iter) {
  239. $Promise.all(iter)['catch'](empty);
  240. })), PROMISE, {
  241. // 25.4.4.1 Promise.all(iterable)
  242. all: function all(iterable) {
  243. var C = this;
  244. var capability = newPromiseCapability(C);
  245. var resolve = capability.resolve;
  246. var reject = capability.reject;
  247. var result = perform(function () {
  248. var values = [];
  249. var index = 0;
  250. var remaining = 1;
  251. forOf(iterable, false, function (promise) {
  252. var $index = index++;
  253. var alreadyCalled = false;
  254. values.push(undefined);
  255. remaining++;
  256. C.resolve(promise).then(function (value) {
  257. if (alreadyCalled) return;
  258. alreadyCalled = true;
  259. values[$index] = value;
  260. --remaining || resolve(values);
  261. }, reject);
  262. });
  263. --remaining || resolve(values);
  264. });
  265. if (result.e) reject(result.v);
  266. return capability.promise;
  267. },
  268. // 25.4.4.4 Promise.race(iterable)
  269. race: function race(iterable) {
  270. var C = this;
  271. var capability = newPromiseCapability(C);
  272. var reject = capability.reject;
  273. var result = perform(function () {
  274. forOf(iterable, false, function (promise) {
  275. C.resolve(promise).then(capability.resolve, reject);
  276. });
  277. });
  278. if (result.e) reject(result.v);
  279. return capability.promise;
  280. }
  281. });