var isObject = require('../internals/is-object');
var aFunction = require('../internals/a-function');
var anInstance = require('../internals/an-instance');
-var classof = require('../internals/classof-raw');
var inspectSource = require('../internals/inspect-source');
var iterate = require('../internals/iterate');
var checkCorrectnessOfIteration = require('../internals/check-correctness-of-iteration');
var InternalStateModule = require('../internals/internal-state');
var isForced = require('../internals/is-forced');
var wellKnownSymbol = require('../internals/well-known-symbol');
+var IS_NODE = require('../internals/engine-is-node');
var V8_VERSION = require('../internals/engine-v8-version');
var SPECIES = wellKnownSymbol('species');
var $fetch = getBuiltIn('fetch');
var newPromiseCapability = newPromiseCapabilityModule.f;
var newGenericPromiseCapability = newPromiseCapability;
-var IS_NODE = classof(process) == 'process';
var DISPATCH_EVENT = !!(document && document.createEvent && global.dispatchEvent);
+var NATIVE_REJECTION_EVENT = typeof PromiseRejectionEvent == 'function';
var UNHANDLED_REJECTION = 'unhandledrejection';
var REJECTION_HANDLED = 'rejectionhandled';
var PENDING = 0;
// We can't detect it synchronously, so just check versions
if (V8_VERSION === 66) return true;
// Unhandled rejections tracking support, NodeJS Promise without it fails @@species test
- if (!IS_NODE && typeof PromiseRejectionEvent != 'function') return true;
+ if (!IS_NODE && !NATIVE_REJECTION_EVENT) return true;
}
// We need Promise#finally in the pure version for preventing prototype pollution
if (IS_PURE && !PromiseConstructor.prototype['finally']) return true;
return isObject(it) && typeof (then = it.then) == 'function' ? then : false;
};
-var notify = function (promise, state, isReject) {
+var notify = function (state, isReject) {
if (state.notified) return;
state.notified = true;
var chain = state.reactions;
try {
if (handler) {
if (!ok) {
- if (state.rejection === UNHANDLED) onHandleUnhandled(promise, state);
+ if (state.rejection === UNHANDLED) onHandleUnhandled(state);
state.rejection = HANDLED;
}
if (handler === true) result = value;
}
state.reactions = [];
state.notified = false;
- if (isReject && !state.rejection) onUnhandled(promise, state);
+ if (isReject && !state.rejection) onUnhandled(state);
});
};
event.initEvent(name, false, true);
global.dispatchEvent(event);
} else event = { promise: promise, reason: reason };
- if (handler = global['on' + name]) handler(event);
+ if (!NATIVE_REJECTION_EVENT && (handler = global['on' + name])) handler(event);
else if (name === UNHANDLED_REJECTION) hostReportErrors('Unhandled promise rejection', reason);
};
-var onUnhandled = function (promise, state) {
+var onUnhandled = function (state) {
task.call(global, function () {
+ var promise = state.facade;
var value = state.value;
var IS_UNHANDLED = isUnhandled(state);
var result;
return state.rejection !== HANDLED && !state.parent;
};
-var onHandleUnhandled = function (promise, state) {
+var onHandleUnhandled = function (state) {
task.call(global, function () {
+ var promise = state.facade;
if (IS_NODE) {
process.emit('rejectionHandled', promise);
} else dispatchEvent(REJECTION_HANDLED, promise, state.value);
});
};
-var bind = function (fn, promise, state, unwrap) {
+var bind = function (fn, state, unwrap) {
return function (value) {
- fn(promise, state, value, unwrap);
+ fn(state, value, unwrap);
};
};
-var internalReject = function (promise, state, value, unwrap) {
+var internalReject = function (state, value, unwrap) {
if (state.done) return;
state.done = true;
if (unwrap) state = unwrap;
state.value = value;
state.state = REJECTED;
- notify(promise, state, true);
+ notify(state, true);
};
-var internalResolve = function (promise, state, value, unwrap) {
+var internalResolve = function (state, value, unwrap) {
if (state.done) return;
state.done = true;
if (unwrap) state = unwrap;
try {
- if (promise === value) throw TypeError("Promise can't be resolved itself");
+ if (state.facade === value) throw TypeError("Promise can't be resolved itself");
var then = isThenable(value);
if (then) {
microtask(function () {
var wrapper = { done: false };
try {
then.call(value,
- bind(internalResolve, promise, wrapper, state),
- bind(internalReject, promise, wrapper, state)
+ bind(internalResolve, wrapper, state),
+ bind(internalReject, wrapper, state)
);
} catch (error) {
- internalReject(promise, wrapper, error, state);
+ internalReject(wrapper, error, state);
}
});
} else {
state.value = value;
state.state = FULFILLED;
- notify(promise, state, false);
+ notify(state, false);
}
} catch (error) {
- internalReject(promise, { done: false }, error, state);
+ internalReject({ done: false }, error, state);
}
};
Internal.call(this);
var state = getInternalState(this);
try {
- executor(bind(internalResolve, this, state), bind(internalReject, this, state));
+ executor(bind(internalResolve, state), bind(internalReject, state));
} catch (error) {
- internalReject(this, state, error);
+ internalReject(state, error);
}
};
// eslint-disable-next-line no-unused-vars
reaction.domain = IS_NODE ? process.domain : undefined;
state.parent = true;
state.reactions.push(reaction);
- if (state.state != PENDING) notify(this, state, false);
+ if (state.state != PENDING) notify(state, false);
return reaction.promise;
},
// `Promise.prototype.catch` method
var promise = new Internal();
var state = getInternalState(promise);
this.promise = promise;
- this.resolve = bind(internalResolve, promise, state);
- this.reject = bind(internalReject, promise, state);
+ this.resolve = bind(internalResolve, state);
+ this.reject = bind(internalReject, state);
};
newPromiseCapabilityModule.f = newPromiseCapability = function (C) {
return C === PromiseConstructor || C === PromiseWrapper