2 var $ = require('../internals/export');
3 var IS_PURE = require('../internals/is-pure');
4 var global = require('../internals/global');
5 var getBuiltIn = require('../internals/get-built-in');
6 var call = require('../internals/function-call');
7 var NativePromise = require('../internals/native-promise-constructor');
8 var redefine = require('../internals/redefine');
9 var redefineAll = require('../internals/redefine-all');
10 var setPrototypeOf = require('../internals/object-set-prototype-of');
11 var setToStringTag = require('../internals/set-to-string-tag');
12 var setSpecies = require('../internals/set-species');
13 var aCallable = require('../internals/a-callable');
14 var isCallable = require('../internals/is-callable');
15 var isObject = require('../internals/is-object');
16 var anInstance = require('../internals/an-instance');
17 var inspectSource = require('../internals/inspect-source');
18 var iterate = require('../internals/iterate');
19 var checkCorrectnessOfIteration = require('../internals/check-correctness-of-iteration');
20 var speciesConstructor = require('../internals/species-constructor');
21 var task = require('../internals/task').set;
22 var microtask = require('../internals/microtask');
23 var promiseResolve = require('../internals/promise-resolve');
24 var hostReportErrors = require('../internals/host-report-errors');
25 var newPromiseCapabilityModule = require('../internals/new-promise-capability');
26 var perform = require('../internals/perform');
27 var InternalStateModule = require('../internals/internal-state');
28 var isForced = require('../internals/is-forced');
29 var wellKnownSymbol = require('../internals/well-known-symbol');
30 var IS_BROWSER = require('../internals/engine-is-browser');
31 var IS_NODE = require('../internals/engine-is-node');
32 var V8_VERSION = require('../internals/engine-v8-version');
34 var SPECIES = wellKnownSymbol('species');
35 var PROMISE = 'Promise';
37 var getInternalState = InternalStateModule.get;
38 var setInternalState = InternalStateModule.set;
39 var getInternalPromiseState = InternalStateModule.getterFor(PROMISE);
40 var NativePromisePrototype = NativePromise && NativePromise.prototype;
41 var PromiseConstructor = NativePromise;
42 var PromisePrototype = NativePromisePrototype;
43 var TypeError = global.TypeError;
44 var document = global.document;
45 var process = global.process;
46 var newPromiseCapability = newPromiseCapabilityModule.f;
47 var newGenericPromiseCapability = newPromiseCapability;
49 var DISPATCH_EVENT = !!(document && document.createEvent && global.dispatchEvent);
50 var NATIVE_REJECTION_EVENT = isCallable(global.PromiseRejectionEvent);
51 var UNHANDLED_REJECTION = 'unhandledrejection';
52 var REJECTION_HANDLED = 'rejectionhandled';
58 var SUBCLASSING = false;
60 var Internal, OwnPromiseCapability, PromiseWrapper, nativeThen;
62 var FORCED = isForced(PROMISE, function () {
63 var PROMISE_CONSTRUCTOR_SOURCE = inspectSource(PromiseConstructor);
64 var GLOBAL_CORE_JS_PROMISE = PROMISE_CONSTRUCTOR_SOURCE !== String(PromiseConstructor);
65 // V8 6.6 (Node 10 and Chrome 66) have a bug with resolving custom thenables
66 // https://bugs.chromium.org/p/chromium/issues/detail?id=830565
67 // We can't detect it synchronously, so just check versions
68 if (!GLOBAL_CORE_JS_PROMISE && V8_VERSION === 66) return true;
69 // We need Promise#finally in the pure version for preventing prototype pollution
70 if (IS_PURE && !PromisePrototype['finally']) return true;
71 // We can't use @@species feature detection in V8 since it causes
72 // deoptimization and performance degradation
73 // https://github.com/zloirock/core-js/issues/679
74 if (V8_VERSION >= 51 && /native code/.test(PROMISE_CONSTRUCTOR_SOURCE)) return false;
75 // Detect correctness of subclassing with @@species support
76 var promise = new PromiseConstructor(function (resolve) { resolve(1); });
77 var FakePromise = function (exec) {
78 exec(function () { /* empty */ }, function () { /* empty */ });
80 var constructor = promise.constructor = {};
81 constructor[SPECIES] = FakePromise;
82 SUBCLASSING = promise.then(function () { /* empty */ }) instanceof FakePromise;
83 if (!SUBCLASSING) return true;
84 // Unhandled rejections tracking support, NodeJS Promise without it fails @@species test
85 return !GLOBAL_CORE_JS_PROMISE && IS_BROWSER && !NATIVE_REJECTION_EVENT;
88 var INCORRECT_ITERATION = FORCED || !checkCorrectnessOfIteration(function (iterable) {
89 PromiseConstructor.all(iterable)['catch'](function () { /* empty */ });
93 var isThenable = function (it) {
95 return isObject(it) && isCallable(then = it.then) ? then : false;
98 var notify = function (state, isReject) {
99 if (state.notified) return;
100 state.notified = true;
101 var chain = state.reactions;
102 microtask(function () {
103 var value = state.value;
104 var ok = state.state == FULFILLED;
106 // variable length - can't use forEach
107 while (chain.length > index) {
108 var reaction = chain[index++];
109 var handler = ok ? reaction.ok : reaction.fail;
110 var resolve = reaction.resolve;
111 var reject = reaction.reject;
112 var domain = reaction.domain;
113 var result, then, exited;
117 if (state.rejection === UNHANDLED) onHandleUnhandled(state);
118 state.rejection = HANDLED;
120 if (handler === true) result = value;
122 if (domain) domain.enter();
123 result = handler(value); // can throw
129 if (result === reaction.promise) {
130 reject(TypeError('Promise-chain cycle'));
131 } else if (then = isThenable(result)) {
132 call(then, result, resolve, reject);
133 } else resolve(result);
134 } else reject(value);
136 if (domain && !exited) domain.exit();
140 state.reactions = [];
141 state.notified = false;
142 if (isReject && !state.rejection) onUnhandled(state);
146 var dispatchEvent = function (name, promise, reason) {
148 if (DISPATCH_EVENT) {
149 event = document.createEvent('Event');
150 event.promise = promise;
151 event.reason = reason;
152 event.initEvent(name, false, true);
153 global.dispatchEvent(event);
154 } else event = { promise: promise, reason: reason };
155 if (!NATIVE_REJECTION_EVENT && (handler = global['on' + name])) handler(event);
156 else if (name === UNHANDLED_REJECTION) hostReportErrors('Unhandled promise rejection', reason);
159 var onUnhandled = function (state) {
160 call(task, global, function () {
161 var promise = state.facade;
162 var value = state.value;
163 var IS_UNHANDLED = isUnhandled(state);
166 result = perform(function () {
168 process.emit('unhandledRejection', value, promise);
169 } else dispatchEvent(UNHANDLED_REJECTION, promise, value);
171 // Browsers should not trigger `rejectionHandled` event if it was handled here, NodeJS - should
172 state.rejection = IS_NODE || isUnhandled(state) ? UNHANDLED : HANDLED;
173 if (result.error) throw result.value;
178 var isUnhandled = function (state) {
179 return state.rejection !== HANDLED && !state.parent;
182 var onHandleUnhandled = function (state) {
183 call(task, global, function () {
184 var promise = state.facade;
186 process.emit('rejectionHandled', promise);
187 } else dispatchEvent(REJECTION_HANDLED, promise, state.value);
191 var bind = function (fn, state, unwrap) {
192 return function (value) {
193 fn(state, value, unwrap);
197 var internalReject = function (state, value, unwrap) {
198 if (state.done) return;
200 if (unwrap) state = unwrap;
202 state.state = REJECTED;
206 var internalResolve = function (state, value, unwrap) {
207 if (state.done) return;
209 if (unwrap) state = unwrap;
211 if (state.facade === value) throw TypeError("Promise can't be resolved itself");
212 var then = isThenable(value);
214 microtask(function () {
215 var wrapper = { done: false };
218 bind(internalResolve, wrapper, state),
219 bind(internalReject, wrapper, state)
222 internalReject(wrapper, error, state);
227 state.state = FULFILLED;
228 notify(state, false);
231 internalReject({ done: false }, error, state);
235 // constructor polyfill
237 // 25.4.3.1 Promise(executor)
238 PromiseConstructor = function Promise(executor) {
239 anInstance(this, PromisePrototype);
241 call(Internal, this);
242 var state = getInternalState(this);
244 executor(bind(internalResolve, state), bind(internalReject, state));
246 internalReject(state, error);
249 PromisePrototype = PromiseConstructor.prototype;
250 // eslint-disable-next-line no-unused-vars -- required for `.length`
251 Internal = function Promise(executor) {
252 setInternalState(this, {
263 Internal.prototype = redefineAll(PromisePrototype, {
264 // `Promise.prototype.then` method
265 // https://tc39.es/ecma262/#sec-promise.prototype.then
266 then: function then(onFulfilled, onRejected) {
267 var state = getInternalPromiseState(this);
268 var reactions = state.reactions;
269 var reaction = newPromiseCapability(speciesConstructor(this, PromiseConstructor));
270 reaction.ok = isCallable(onFulfilled) ? onFulfilled : true;
271 reaction.fail = isCallable(onRejected) && onRejected;
272 reaction.domain = IS_NODE ? process.domain : undefined;
274 reactions[reactions.length] = reaction;
275 if (state.state != PENDING) notify(state, false);
276 return reaction.promise;
278 // `Promise.prototype.catch` method
279 // https://tc39.es/ecma262/#sec-promise.prototype.catch
280 'catch': function (onRejected) {
281 return this.then(undefined, onRejected);
284 OwnPromiseCapability = function () {
285 var promise = new Internal();
286 var state = getInternalState(promise);
287 this.promise = promise;
288 this.resolve = bind(internalResolve, state);
289 this.reject = bind(internalReject, state);
291 newPromiseCapabilityModule.f = newPromiseCapability = function (C) {
292 return C === PromiseConstructor || C === PromiseWrapper
293 ? new OwnPromiseCapability(C)
294 : newGenericPromiseCapability(C);
297 if (!IS_PURE && isCallable(NativePromise) && NativePromisePrototype !== Object.prototype) {
298 nativeThen = NativePromisePrototype.then;
301 // make `Promise#then` return a polyfilled `Promise` for native promise-based APIs
302 redefine(NativePromisePrototype, 'then', function then(onFulfilled, onRejected) {
304 return new PromiseConstructor(function (resolve, reject) {
305 call(nativeThen, that, resolve, reject);
306 }).then(onFulfilled, onRejected);
307 // https://github.com/zloirock/core-js/issues/640
308 }, { unsafe: true });
310 // makes sure that native promise-based APIs `Promise#catch` properly works with patched `Promise#then`
311 redefine(NativePromisePrototype, 'catch', PromisePrototype['catch'], { unsafe: true });
314 // make `.constructor === Promise` work for native promise-based APIs
316 delete NativePromisePrototype.constructor;
317 } catch (error) { /* empty */ }
319 // make `instanceof Promise` work for native promise-based APIs
320 if (setPrototypeOf) {
321 setPrototypeOf(NativePromisePrototype, PromisePrototype);
326 $({ global: true, wrap: true, forced: FORCED }, {
327 Promise: PromiseConstructor
330 setToStringTag(PromiseConstructor, PROMISE, false, true);
333 PromiseWrapper = getBuiltIn(PROMISE);
336 $({ target: PROMISE, stat: true, forced: FORCED }, {
337 // `Promise.reject` method
338 // https://tc39.es/ecma262/#sec-promise.reject
339 reject: function reject(r) {
340 var capability = newPromiseCapability(this);
341 call(capability.reject, undefined, r);
342 return capability.promise;
346 $({ target: PROMISE, stat: true, forced: IS_PURE || FORCED }, {
347 // `Promise.resolve` method
348 // https://tc39.es/ecma262/#sec-promise.resolve
349 resolve: function resolve(x) {
350 return promiseResolve(IS_PURE && this === PromiseWrapper ? PromiseConstructor : this, x);
354 $({ target: PROMISE, stat: true, forced: INCORRECT_ITERATION }, {
355 // `Promise.all` method
356 // https://tc39.es/ecma262/#sec-promise.all
357 all: function all(iterable) {
359 var capability = newPromiseCapability(C);
360 var resolve = capability.resolve;
361 var reject = capability.reject;
362 var result = perform(function () {
363 var $promiseResolve = aCallable(C.resolve);
367 iterate(iterable, function (promise) {
368 var index = counter++;
369 var alreadyCalled = false;
371 call($promiseResolve, C, promise).then(function (value) {
372 if (alreadyCalled) return;
373 alreadyCalled = true;
374 values[index] = value;
375 --remaining || resolve(values);
378 --remaining || resolve(values);
380 if (result.error) reject(result.value);
381 return capability.promise;
383 // `Promise.race` method
384 // https://tc39.es/ecma262/#sec-promise.race
385 race: function race(iterable) {
387 var capability = newPromiseCapability(C);
388 var reject = capability.reject;
389 var result = perform(function () {
390 var $promiseResolve = aCallable(C.resolve);
391 iterate(iterable, function (promise) {
392 call($promiseResolve, C, promise).then(capability.resolve, reject);
395 if (result.error) reject(result.value);
396 return capability.promise;