es.promise.constructor.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var IS_PURE = require('../internals/is-pure');
  4. var IS_NODE = require('../internals/engine-is-node');
  5. var global = require('../internals/global');
  6. var call = require('../internals/function-call');
  7. var defineBuiltIn = require('../internals/define-built-in');
  8. var setPrototypeOf = require('../internals/object-set-prototype-of');
  9. var setToStringTag = require('../internals/set-to-string-tag');
  10. var setSpecies = require('../internals/set-species');
  11. var aCallable = require('../internals/a-callable');
  12. var isCallable = require('../internals/is-callable');
  13. var isObject = require('../internals/is-object');
  14. var anInstance = require('../internals/an-instance');
  15. var speciesConstructor = require('../internals/species-constructor');
  16. var task = require('../internals/task').set;
  17. var microtask = require('../internals/microtask');
  18. var hostReportErrors = require('../internals/host-report-errors');
  19. var perform = require('../internals/perform');
  20. var Queue = require('../internals/queue');
  21. var InternalStateModule = require('../internals/internal-state');
  22. var NativePromiseConstructor = require('../internals/promise-native-constructor');
  23. var PromiseConstructorDetection = require('../internals/promise-constructor-detection');
  24. var newPromiseCapabilityModule = require('../internals/new-promise-capability');
  25. var PROMISE = 'Promise';
  26. var FORCED_PROMISE_CONSTRUCTOR = PromiseConstructorDetection.CONSTRUCTOR;
  27. var NATIVE_PROMISE_REJECTION_EVENT = PromiseConstructorDetection.REJECTION_EVENT;
  28. var NATIVE_PROMISE_SUBCLASSING = PromiseConstructorDetection.SUBCLASSING;
  29. var getInternalPromiseState = InternalStateModule.getterFor(PROMISE);
  30. var setInternalState = InternalStateModule.set;
  31. var NativePromisePrototype = NativePromiseConstructor && NativePromiseConstructor.prototype;
  32. var PromiseConstructor = NativePromiseConstructor;
  33. var PromisePrototype = NativePromisePrototype;
  34. var TypeError = global.TypeError;
  35. var document = global.document;
  36. var process = global.process;
  37. var newPromiseCapability = newPromiseCapabilityModule.f;
  38. var newGenericPromiseCapability = newPromiseCapability;
  39. var DISPATCH_EVENT = !!(document && document.createEvent && global.dispatchEvent);
  40. var UNHANDLED_REJECTION = 'unhandledrejection';
  41. var REJECTION_HANDLED = 'rejectionhandled';
  42. var PENDING = 0;
  43. var FULFILLED = 1;
  44. var REJECTED = 2;
  45. var HANDLED = 1;
  46. var UNHANDLED = 2;
  47. var Internal, OwnPromiseCapability, PromiseWrapper, nativeThen;
  48. // helpers
  49. var isThenable = function (it) {
  50. var then;
  51. return isObject(it) && isCallable(then = it.then) ? then : false;
  52. };
  53. var callReaction = function (reaction, state) {
  54. var value = state.value;
  55. var ok = state.state == FULFILLED;
  56. var handler = ok ? reaction.ok : reaction.fail;
  57. var resolve = reaction.resolve;
  58. var reject = reaction.reject;
  59. var domain = reaction.domain;
  60. var result, then, exited;
  61. try {
  62. if (handler) {
  63. if (!ok) {
  64. if (state.rejection === UNHANDLED) onHandleUnhandled(state);
  65. state.rejection = HANDLED;
  66. }
  67. if (handler === true) result = value;
  68. else {
  69. if (domain) domain.enter();
  70. result = handler(value); // can throw
  71. if (domain) {
  72. domain.exit();
  73. exited = true;
  74. }
  75. }
  76. if (result === reaction.promise) {
  77. reject(TypeError('Promise-chain cycle'));
  78. } else if (then = isThenable(result)) {
  79. call(then, result, resolve, reject);
  80. } else resolve(result);
  81. } else reject(value);
  82. } catch (error) {
  83. if (domain && !exited) domain.exit();
  84. reject(error);
  85. }
  86. };
  87. var notify = function (state, isReject) {
  88. if (state.notified) return;
  89. state.notified = true;
  90. microtask(function () {
  91. var reactions = state.reactions;
  92. var reaction;
  93. while (reaction = reactions.get()) {
  94. callReaction(reaction, state);
  95. }
  96. state.notified = false;
  97. if (isReject && !state.rejection) onUnhandled(state);
  98. });
  99. };
  100. var dispatchEvent = function (name, promise, reason) {
  101. var event, handler;
  102. if (DISPATCH_EVENT) {
  103. event = document.createEvent('Event');
  104. event.promise = promise;
  105. event.reason = reason;
  106. event.initEvent(name, false, true);
  107. global.dispatchEvent(event);
  108. } else event = { promise: promise, reason: reason };
  109. if (!NATIVE_PROMISE_REJECTION_EVENT && (handler = global['on' + name])) handler(event);
  110. else if (name === UNHANDLED_REJECTION) hostReportErrors('Unhandled promise rejection', reason);
  111. };
  112. var onUnhandled = function (state) {
  113. call(task, global, function () {
  114. var promise = state.facade;
  115. var value = state.value;
  116. var IS_UNHANDLED = isUnhandled(state);
  117. var result;
  118. if (IS_UNHANDLED) {
  119. result = perform(function () {
  120. if (IS_NODE) {
  121. process.emit('unhandledRejection', value, promise);
  122. } else dispatchEvent(UNHANDLED_REJECTION, promise, value);
  123. });
  124. // Browsers should not trigger `rejectionHandled` event if it was handled here, NodeJS - should
  125. state.rejection = IS_NODE || isUnhandled(state) ? UNHANDLED : HANDLED;
  126. if (result.error) throw result.value;
  127. }
  128. });
  129. };
  130. var isUnhandled = function (state) {
  131. return state.rejection !== HANDLED && !state.parent;
  132. };
  133. var onHandleUnhandled = function (state) {
  134. call(task, global, function () {
  135. var promise = state.facade;
  136. if (IS_NODE) {
  137. process.emit('rejectionHandled', promise);
  138. } else dispatchEvent(REJECTION_HANDLED, promise, state.value);
  139. });
  140. };
  141. var bind = function (fn, state, unwrap) {
  142. return function (value) {
  143. fn(state, value, unwrap);
  144. };
  145. };
  146. var internalReject = function (state, value, unwrap) {
  147. if (state.done) return;
  148. state.done = true;
  149. if (unwrap) state = unwrap;
  150. state.value = value;
  151. state.state = REJECTED;
  152. notify(state, true);
  153. };
  154. var internalResolve = function (state, value, unwrap) {
  155. if (state.done) return;
  156. state.done = true;
  157. if (unwrap) state = unwrap;
  158. try {
  159. if (state.facade === value) throw TypeError("Promise can't be resolved itself");
  160. var then = isThenable(value);
  161. if (then) {
  162. microtask(function () {
  163. var wrapper = { done: false };
  164. try {
  165. call(then, value,
  166. bind(internalResolve, wrapper, state),
  167. bind(internalReject, wrapper, state)
  168. );
  169. } catch (error) {
  170. internalReject(wrapper, error, state);
  171. }
  172. });
  173. } else {
  174. state.value = value;
  175. state.state = FULFILLED;
  176. notify(state, false);
  177. }
  178. } catch (error) {
  179. internalReject({ done: false }, error, state);
  180. }
  181. };
  182. // constructor polyfill
  183. if (FORCED_PROMISE_CONSTRUCTOR) {
  184. // 25.4.3.1 Promise(executor)
  185. PromiseConstructor = function Promise(executor) {
  186. anInstance(this, PromisePrototype);
  187. aCallable(executor);
  188. call(Internal, this);
  189. var state = getInternalPromiseState(this);
  190. try {
  191. executor(bind(internalResolve, state), bind(internalReject, state));
  192. } catch (error) {
  193. internalReject(state, error);
  194. }
  195. };
  196. PromisePrototype = PromiseConstructor.prototype;
  197. // eslint-disable-next-line no-unused-vars -- required for `.length`
  198. Internal = function Promise(executor) {
  199. setInternalState(this, {
  200. type: PROMISE,
  201. done: false,
  202. notified: false,
  203. parent: false,
  204. reactions: new Queue(),
  205. rejection: false,
  206. state: PENDING,
  207. value: undefined
  208. });
  209. };
  210. // `Promise.prototype.then` method
  211. // https://tc39.es/ecma262/#sec-promise.prototype.then
  212. Internal.prototype = defineBuiltIn(PromisePrototype, 'then', function then(onFulfilled, onRejected) {
  213. var state = getInternalPromiseState(this);
  214. var reaction = newPromiseCapability(speciesConstructor(this, PromiseConstructor));
  215. state.parent = true;
  216. reaction.ok = isCallable(onFulfilled) ? onFulfilled : true;
  217. reaction.fail = isCallable(onRejected) && onRejected;
  218. reaction.domain = IS_NODE ? process.domain : undefined;
  219. if (state.state == PENDING) state.reactions.add(reaction);
  220. else microtask(function () {
  221. callReaction(reaction, state);
  222. });
  223. return reaction.promise;
  224. });
  225. OwnPromiseCapability = function () {
  226. var promise = new Internal();
  227. var state = getInternalPromiseState(promise);
  228. this.promise = promise;
  229. this.resolve = bind(internalResolve, state);
  230. this.reject = bind(internalReject, state);
  231. };
  232. newPromiseCapabilityModule.f = newPromiseCapability = function (C) {
  233. return C === PromiseConstructor || C === PromiseWrapper
  234. ? new OwnPromiseCapability(C)
  235. : newGenericPromiseCapability(C);
  236. };
  237. if (!IS_PURE && isCallable(NativePromiseConstructor) && NativePromisePrototype !== Object.prototype) {
  238. nativeThen = NativePromisePrototype.then;
  239. if (!NATIVE_PROMISE_SUBCLASSING) {
  240. // make `Promise#then` return a polyfilled `Promise` for native promise-based APIs
  241. defineBuiltIn(NativePromisePrototype, 'then', function then(onFulfilled, onRejected) {
  242. var that = this;
  243. return new PromiseConstructor(function (resolve, reject) {
  244. call(nativeThen, that, resolve, reject);
  245. }).then(onFulfilled, onRejected);
  246. // https://github.com/zloirock/core-js/issues/640
  247. }, { unsafe: true });
  248. }
  249. // make `.constructor === Promise` work for native promise-based APIs
  250. try {
  251. delete NativePromisePrototype.constructor;
  252. } catch (error) { /* empty */ }
  253. // make `instanceof Promise` work for native promise-based APIs
  254. if (setPrototypeOf) {
  255. setPrototypeOf(NativePromisePrototype, PromisePrototype);
  256. }
  257. }
  258. }
  259. $({ global: true, constructor: true, wrap: true, forced: FORCED_PROMISE_CONSTRUCTOR }, {
  260. Promise: PromiseConstructor
  261. });
  262. setToStringTag(PromiseConstructor, PROMISE, false, true);
  263. setSpecies(PROMISE);