2016-10-10 03:43:44 +02:00
|
|
|
'use strict';
|
2017-08-14 05:01:11 +02:00
|
|
|
var LIBRARY = require('./_library');
|
|
|
|
var global = require('./_global');
|
|
|
|
var ctx = require('./_ctx');
|
|
|
|
var classof = require('./_classof');
|
|
|
|
var $export = require('./_export');
|
|
|
|
var isObject = require('./_is-object');
|
|
|
|
var aFunction = require('./_a-function');
|
|
|
|
var anInstance = require('./_an-instance');
|
|
|
|
var forOf = require('./_for-of');
|
|
|
|
var speciesConstructor = require('./_species-constructor');
|
|
|
|
var task = require('./_task').set;
|
|
|
|
var microtask = require('./_microtask')();
|
|
|
|
var newPromiseCapabilityModule = require('./_new-promise-capability');
|
|
|
|
var perform = require('./_perform');
|
|
|
|
var promiseResolve = require('./_promise-resolve');
|
|
|
|
var PROMISE = 'Promise';
|
|
|
|
var TypeError = global.TypeError;
|
|
|
|
var process = global.process;
|
|
|
|
var $Promise = global[PROMISE];
|
|
|
|
var isNode = classof(process) == 'process';
|
|
|
|
var empty = function () { /* empty */ };
|
|
|
|
var Internal, newGenericPromiseCapability, OwnPromiseCapability, Wrapper;
|
|
|
|
var newPromiseCapability = newGenericPromiseCapability = newPromiseCapabilityModule.f;
|
2016-10-10 03:43:44 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
var USE_NATIVE = !!function () {
|
2016-10-10 03:43:44 +02:00
|
|
|
try {
|
2017-05-27 17:36:13 +02:00
|
|
|
// correct subclassing with @@species support
|
2017-08-14 05:01:11 +02:00
|
|
|
var promise = $Promise.resolve(1);
|
|
|
|
var FakePromise = (promise.constructor = {})[require('./_wks')('species')] = function (exec) {
|
|
|
|
exec(empty, empty);
|
|
|
|
};
|
2017-05-27 17:36:13 +02:00
|
|
|
// unhandled rejections tracking support, NodeJS Promise without it fails @@species test
|
|
|
|
return (isNode || typeof PromiseRejectionEvent == 'function') && promise.then(empty) instanceof FakePromise;
|
2017-08-14 05:01:11 +02:00
|
|
|
} catch (e) { /* empty */ }
|
2016-10-10 03:43:44 +02:00
|
|
|
}();
|
|
|
|
|
|
|
|
// helpers
|
2017-08-14 05:01:11 +02:00
|
|
|
var isThenable = function (it) {
|
2016-10-10 03:43:44 +02:00
|
|
|
var then;
|
|
|
|
return isObject(it) && typeof (then = it.then) == 'function' ? then : false;
|
|
|
|
};
|
2017-08-14 05:01:11 +02:00
|
|
|
var notify = function (promise, isReject) {
|
|
|
|
if (promise._n) return;
|
2017-05-27 17:36:13 +02:00
|
|
|
promise._n = true;
|
|
|
|
var chain = promise._c;
|
2017-08-14 05:01:11 +02:00
|
|
|
microtask(function () {
|
|
|
|
var value = promise._v;
|
|
|
|
var ok = promise._s == 1;
|
|
|
|
var i = 0;
|
|
|
|
var run = function (reaction) {
|
|
|
|
var handler = ok ? reaction.ok : reaction.fail;
|
|
|
|
var resolve = reaction.resolve;
|
|
|
|
var reject = reaction.reject;
|
|
|
|
var domain = reaction.domain;
|
|
|
|
var result, then;
|
2016-10-10 03:43:44 +02:00
|
|
|
try {
|
2017-08-14 05:01:11 +02:00
|
|
|
if (handler) {
|
|
|
|
if (!ok) {
|
|
|
|
if (promise._h == 2) onHandleUnhandled(promise);
|
2017-05-27 17:36:13 +02:00
|
|
|
promise._h = 1;
|
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
if (handler === true) result = value;
|
2017-05-27 17:36:13 +02:00
|
|
|
else {
|
2017-08-14 05:01:11 +02:00
|
|
|
if (domain) domain.enter();
|
2017-05-27 17:36:13 +02:00
|
|
|
result = handler(value);
|
2017-08-14 05:01:11 +02:00
|
|
|
if (domain) domain.exit();
|
2017-05-27 17:36:13 +02:00
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
if (result === reaction.promise) {
|
2016-10-10 03:43:44 +02:00
|
|
|
reject(TypeError('Promise-chain cycle'));
|
2017-08-14 05:01:11 +02:00
|
|
|
} else if (then = isThenable(result)) {
|
2016-10-10 03:43:44 +02:00
|
|
|
then.call(result, resolve, reject);
|
|
|
|
} else resolve(result);
|
|
|
|
} else reject(value);
|
2017-08-14 05:01:11 +02:00
|
|
|
} catch (e) {
|
2016-10-10 03:43:44 +02:00
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
};
|
2017-08-14 05:01:11 +02:00
|
|
|
while (chain.length > i) run(chain[i++]); // variable length - can't use forEach
|
2017-05-27 17:36:13 +02:00
|
|
|
promise._c = [];
|
|
|
|
promise._n = false;
|
2017-08-14 05:01:11 +02:00
|
|
|
if (isReject && !promise._h) onUnhandled(promise);
|
2017-05-27 17:36:13 +02:00
|
|
|
});
|
|
|
|
};
|
2017-08-14 05:01:11 +02:00
|
|
|
var onUnhandled = function (promise) {
|
|
|
|
task.call(global, function () {
|
|
|
|
var value = promise._v;
|
|
|
|
var unhandled = isUnhandled(promise);
|
|
|
|
var result, handler, console;
|
|
|
|
if (unhandled) {
|
|
|
|
result = perform(function () {
|
|
|
|
if (isNode) {
|
2016-10-10 03:43:44 +02:00
|
|
|
process.emit('unhandledRejection', value, promise);
|
2017-08-14 05:01:11 +02:00
|
|
|
} else if (handler = global.onunhandledrejection) {
|
|
|
|
handler({ promise: promise, reason: value });
|
|
|
|
} else if ((console = global.console) && console.error) {
|
2016-10-10 03:43:44 +02:00
|
|
|
console.error('Unhandled promise rejection', value);
|
|
|
|
}
|
2017-05-27 17:36:13 +02:00
|
|
|
});
|
|
|
|
// Browsers should not trigger `rejectionHandled` event if it was handled here, NodeJS - should
|
|
|
|
promise._h = isNode || isUnhandled(promise) ? 2 : 1;
|
|
|
|
} promise._a = undefined;
|
2017-08-14 05:01:11 +02:00
|
|
|
if (unhandled && result.e) throw result.v;
|
2016-10-10 03:43:44 +02:00
|
|
|
});
|
|
|
|
};
|
2017-08-14 05:01:11 +02:00
|
|
|
var isUnhandled = function (promise) {
|
2017-12-27 19:33:54 +01:00
|
|
|
return promise._h !== 1 && (promise._a || promise._c).length === 0;
|
2016-10-10 03:43:44 +02:00
|
|
|
};
|
2017-08-14 05:01:11 +02:00
|
|
|
var onHandleUnhandled = function (promise) {
|
|
|
|
task.call(global, function () {
|
2017-05-27 17:36:13 +02:00
|
|
|
var handler;
|
2017-08-14 05:01:11 +02:00
|
|
|
if (isNode) {
|
2017-05-27 17:36:13 +02:00
|
|
|
process.emit('rejectionHandled', promise);
|
2017-08-14 05:01:11 +02:00
|
|
|
} else if (handler = global.onrejectionhandled) {
|
|
|
|
handler({ promise: promise, reason: promise._v });
|
2017-05-27 17:36:13 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2017-08-14 05:01:11 +02:00
|
|
|
var $reject = function (value) {
|
2017-05-27 17:36:13 +02:00
|
|
|
var promise = this;
|
2017-08-14 05:01:11 +02:00
|
|
|
if (promise._d) return;
|
2017-05-27 17:36:13 +02:00
|
|
|
promise._d = true;
|
|
|
|
promise = promise._w || promise; // unwrap
|
|
|
|
promise._v = value;
|
|
|
|
promise._s = 2;
|
2017-08-14 05:01:11 +02:00
|
|
|
if (!promise._a) promise._a = promise._c.slice();
|
2017-05-27 17:36:13 +02:00
|
|
|
notify(promise, true);
|
2016-10-10 03:43:44 +02:00
|
|
|
};
|
2017-08-14 05:01:11 +02:00
|
|
|
var $resolve = function (value) {
|
|
|
|
var promise = this;
|
|
|
|
var then;
|
|
|
|
if (promise._d) return;
|
2017-05-27 17:36:13 +02:00
|
|
|
promise._d = true;
|
|
|
|
promise = promise._w || promise; // unwrap
|
2016-10-10 03:43:44 +02:00
|
|
|
try {
|
2017-08-14 05:01:11 +02:00
|
|
|
if (promise === value) throw TypeError("Promise can't be resolved itself");
|
|
|
|
if (then = isThenable(value)) {
|
|
|
|
microtask(function () {
|
|
|
|
var wrapper = { _w: promise, _d: false }; // wrap
|
2016-10-10 03:43:44 +02:00
|
|
|
try {
|
|
|
|
then.call(value, ctx($resolve, wrapper, 1), ctx($reject, wrapper, 1));
|
2017-08-14 05:01:11 +02:00
|
|
|
} catch (e) {
|
2016-10-10 03:43:44 +02:00
|
|
|
$reject.call(wrapper, e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2017-05-27 17:36:13 +02:00
|
|
|
promise._v = value;
|
|
|
|
promise._s = 1;
|
|
|
|
notify(promise, false);
|
2016-10-10 03:43:44 +02:00
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
} catch (e) {
|
|
|
|
$reject.call({ _w: promise, _d: false }, e); // wrap
|
2016-10-10 03:43:44 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// constructor polyfill
|
2017-08-14 05:01:11 +02:00
|
|
|
if (!USE_NATIVE) {
|
2016-10-10 03:43:44 +02:00
|
|
|
// 25.4.3.1 Promise(executor)
|
2017-08-14 05:01:11 +02:00
|
|
|
$Promise = function Promise(executor) {
|
2017-05-27 17:36:13 +02:00
|
|
|
anInstance(this, $Promise, PROMISE, '_h');
|
2016-10-10 03:43:44 +02:00
|
|
|
aFunction(executor);
|
2017-05-27 17:36:13 +02:00
|
|
|
Internal.call(this);
|
2016-10-10 03:43:44 +02:00
|
|
|
try {
|
2017-05-27 17:36:13 +02:00
|
|
|
executor(ctx($resolve, this, 1), ctx($reject, this, 1));
|
2017-08-14 05:01:11 +02:00
|
|
|
} catch (err) {
|
2017-05-27 17:36:13 +02:00
|
|
|
$reject.call(this, err);
|
2016-10-10 03:43:44 +02:00
|
|
|
}
|
|
|
|
};
|
2017-08-14 05:01:11 +02:00
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
Internal = function Promise(executor) {
|
2017-05-27 17:36:13 +02:00
|
|
|
this._c = []; // <- awaiting reactions
|
|
|
|
this._a = undefined; // <- checked in isUnhandled reactions
|
|
|
|
this._s = 0; // <- state
|
|
|
|
this._d = false; // <- done
|
|
|
|
this._v = undefined; // <- value
|
|
|
|
this._h = 0; // <- rejection state, 0 - default, 1 - handled, 2 - unhandled
|
|
|
|
this._n = false; // <- notify
|
|
|
|
};
|
|
|
|
Internal.prototype = require('./_redefine-all')($Promise.prototype, {
|
2016-10-10 03:43:44 +02:00
|
|
|
// 25.4.5.3 Promise.prototype.then(onFulfilled, onRejected)
|
2017-08-14 05:01:11 +02:00
|
|
|
then: function then(onFulfilled, onRejected) {
|
|
|
|
var reaction = newPromiseCapability(speciesConstructor(this, $Promise));
|
|
|
|
reaction.ok = typeof onFulfilled == 'function' ? onFulfilled : true;
|
|
|
|
reaction.fail = typeof onRejected == 'function' && onRejected;
|
2017-05-27 17:36:13 +02:00
|
|
|
reaction.domain = isNode ? process.domain : undefined;
|
|
|
|
this._c.push(reaction);
|
2017-08-14 05:01:11 +02:00
|
|
|
if (this._a) this._a.push(reaction);
|
|
|
|
if (this._s) notify(this, false);
|
2017-05-27 17:36:13 +02:00
|
|
|
return reaction.promise;
|
2016-10-10 03:43:44 +02:00
|
|
|
},
|
|
|
|
// 25.4.5.1 Promise.prototype.catch(onRejected)
|
2017-08-14 05:01:11 +02:00
|
|
|
'catch': function (onRejected) {
|
2016-10-10 03:43:44 +02:00
|
|
|
return this.then(undefined, onRejected);
|
|
|
|
}
|
|
|
|
});
|
2017-08-14 05:01:11 +02:00
|
|
|
OwnPromiseCapability = function () {
|
|
|
|
var promise = new Internal();
|
2017-05-27 17:36:13 +02:00
|
|
|
this.promise = promise;
|
|
|
|
this.resolve = ctx($resolve, promise, 1);
|
2017-08-14 05:01:11 +02:00
|
|
|
this.reject = ctx($reject, promise, 1);
|
|
|
|
};
|
|
|
|
newPromiseCapabilityModule.f = newPromiseCapability = function (C) {
|
2017-12-27 19:33:54 +01:00
|
|
|
return C === $Promise || C === Wrapper
|
2017-08-14 05:01:11 +02:00
|
|
|
? new OwnPromiseCapability(C)
|
|
|
|
: newGenericPromiseCapability(C);
|
2017-05-27 17:36:13 +02:00
|
|
|
};
|
2016-10-10 03:43:44 +02:00
|
|
|
}
|
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
$export($export.G + $export.W + $export.F * !USE_NATIVE, { Promise: $Promise });
|
2017-05-27 17:36:13 +02:00
|
|
|
require('./_set-to-string-tag')($Promise, PROMISE);
|
|
|
|
require('./_set-species')(PROMISE);
|
|
|
|
Wrapper = require('./_core')[PROMISE];
|
2016-10-10 03:43:44 +02:00
|
|
|
|
|
|
|
// statics
|
|
|
|
$export($export.S + $export.F * !USE_NATIVE, PROMISE, {
|
|
|
|
// 25.4.4.5 Promise.reject(r)
|
2017-08-14 05:01:11 +02:00
|
|
|
reject: function reject(r) {
|
|
|
|
var capability = newPromiseCapability(this);
|
|
|
|
var $$reject = capability.reject;
|
2016-10-10 03:43:44 +02:00
|
|
|
$$reject(r);
|
|
|
|
return capability.promise;
|
|
|
|
}
|
|
|
|
});
|
2017-05-27 17:36:13 +02:00
|
|
|
$export($export.S + $export.F * (LIBRARY || !USE_NATIVE), PROMISE, {
|
2016-10-10 03:43:44 +02:00
|
|
|
// 25.4.4.6 Promise.resolve(x)
|
2017-08-14 05:01:11 +02:00
|
|
|
resolve: function resolve(x) {
|
2017-12-27 19:33:54 +01:00
|
|
|
return promiseResolve(LIBRARY && this === Wrapper ? $Promise : this, x);
|
2016-10-10 03:43:44 +02:00
|
|
|
}
|
|
|
|
});
|
2017-08-14 05:01:11 +02:00
|
|
|
$export($export.S + $export.F * !(USE_NATIVE && require('./_iter-detect')(function (iter) {
|
2017-05-27 17:36:13 +02:00
|
|
|
$Promise.all(iter)['catch'](empty);
|
2016-10-10 03:43:44 +02:00
|
|
|
})), PROMISE, {
|
|
|
|
// 25.4.4.1 Promise.all(iterable)
|
2017-08-14 05:01:11 +02:00
|
|
|
all: function all(iterable) {
|
|
|
|
var C = this;
|
|
|
|
var capability = newPromiseCapability(C);
|
|
|
|
var resolve = capability.resolve;
|
|
|
|
var reject = capability.reject;
|
|
|
|
var result = perform(function () {
|
|
|
|
var values = [];
|
|
|
|
var index = 0;
|
|
|
|
var remaining = 1;
|
|
|
|
forOf(iterable, false, function (promise) {
|
|
|
|
var $index = index++;
|
|
|
|
var alreadyCalled = false;
|
2017-05-27 17:36:13 +02:00
|
|
|
values.push(undefined);
|
|
|
|
remaining++;
|
2017-08-14 05:01:11 +02:00
|
|
|
C.resolve(promise).then(function (value) {
|
|
|
|
if (alreadyCalled) return;
|
|
|
|
alreadyCalled = true;
|
2017-05-27 17:36:13 +02:00
|
|
|
values[$index] = value;
|
|
|
|
--remaining || resolve(values);
|
2016-10-10 03:43:44 +02:00
|
|
|
}, reject);
|
|
|
|
});
|
2017-05-27 17:36:13 +02:00
|
|
|
--remaining || resolve(values);
|
2016-10-10 03:43:44 +02:00
|
|
|
});
|
2017-08-14 05:01:11 +02:00
|
|
|
if (result.e) reject(result.v);
|
2016-10-10 03:43:44 +02:00
|
|
|
return capability.promise;
|
|
|
|
},
|
|
|
|
// 25.4.4.4 Promise.race(iterable)
|
2017-08-14 05:01:11 +02:00
|
|
|
race: function race(iterable) {
|
|
|
|
var C = this;
|
|
|
|
var capability = newPromiseCapability(C);
|
|
|
|
var reject = capability.reject;
|
|
|
|
var result = perform(function () {
|
|
|
|
forOf(iterable, false, function (promise) {
|
2016-10-10 03:43:44 +02:00
|
|
|
C.resolve(promise).then(capability.resolve, reject);
|
|
|
|
});
|
|
|
|
});
|
2017-08-14 05:01:11 +02:00
|
|
|
if (result.e) reject(result.v);
|
2016-10-10 03:43:44 +02:00
|
|
|
return capability.promise;
|
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
});
|