2016-10-10 03:43:44 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function noop() {}
|
|
|
|
|
|
|
|
// States:
|
|
|
|
//
|
|
|
|
// 0 - pending
|
|
|
|
// 1 - fulfilled with _value
|
|
|
|
// 2 - rejected with _value
|
|
|
|
// 3 - adopted the state of another promise, _value
|
|
|
|
//
|
|
|
|
// once the state is no longer pending (0) it is immutable
|
|
|
|
|
|
|
|
// All `_` prefixed properties will be reduced to `_{random number}`
|
|
|
|
// at build time to obfuscate them and discourage their use.
|
|
|
|
// We don't use symbols or Object.defineProperty to fully hide them
|
|
|
|
// because the performance isn't good enough.
|
|
|
|
|
|
|
|
|
|
|
|
// to avoid using try/catch inside critical functions, we
|
|
|
|
// extract them to here.
|
|
|
|
var LAST_ERROR = null;
|
|
|
|
var IS_ERROR = {};
|
|
|
|
function getThen(obj) {
|
|
|
|
try {
|
|
|
|
return obj.then;
|
|
|
|
} catch (ex) {
|
|
|
|
LAST_ERROR = ex;
|
|
|
|
return IS_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function tryCallOne(fn, a) {
|
|
|
|
try {
|
|
|
|
return fn(a);
|
|
|
|
} catch (ex) {
|
|
|
|
LAST_ERROR = ex;
|
|
|
|
return IS_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function tryCallTwo(fn, a, b) {
|
|
|
|
try {
|
|
|
|
fn(a, b);
|
|
|
|
} catch (ex) {
|
|
|
|
LAST_ERROR = ex;
|
|
|
|
return IS_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Promise;
|
|
|
|
|
|
|
|
function Promise(fn) {
|
|
|
|
if (typeof this !== 'object') {
|
|
|
|
throw new TypeError('Promises must be constructed via new');
|
|
|
|
}
|
|
|
|
if (typeof fn !== 'function') {
|
2017-08-14 05:01:11 +02:00
|
|
|
throw new TypeError('Promise constructor\'s argument is not a function');
|
2016-10-10 03:43:44 +02:00
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
this._75 = 0;
|
|
|
|
this._83 = 0;
|
|
|
|
this._18 = null;
|
|
|
|
this._38 = null;
|
2016-10-10 03:43:44 +02:00
|
|
|
if (fn === noop) return;
|
|
|
|
doResolve(fn, this);
|
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
Promise._47 = null;
|
|
|
|
Promise._71 = null;
|
|
|
|
Promise._44 = noop;
|
2016-10-10 03:43:44 +02:00
|
|
|
|
|
|
|
Promise.prototype.then = function(onFulfilled, onRejected) {
|
|
|
|
if (this.constructor !== Promise) {
|
|
|
|
return safeThen(this, onFulfilled, onRejected);
|
|
|
|
}
|
|
|
|
var res = new Promise(noop);
|
|
|
|
handle(this, new Handler(onFulfilled, onRejected, res));
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
|
|
|
|
function safeThen(self, onFulfilled, onRejected) {
|
|
|
|
return new self.constructor(function (resolve, reject) {
|
|
|
|
var res = new Promise(noop);
|
|
|
|
res.then(resolve, reject);
|
|
|
|
handle(self, new Handler(onFulfilled, onRejected, res));
|
|
|
|
});
|
2017-08-14 05:01:11 +02:00
|
|
|
}
|
2016-10-10 03:43:44 +02:00
|
|
|
function handle(self, deferred) {
|
2017-12-10 21:51:33 +01:00
|
|
|
while (self._83 === 3) {
|
|
|
|
self = self._18;
|
2016-10-10 03:43:44 +02:00
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
if (Promise._47) {
|
|
|
|
Promise._47(self);
|
2016-10-10 03:43:44 +02:00
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
if (self._83 === 0) {
|
|
|
|
if (self._75 === 0) {
|
|
|
|
self._75 = 1;
|
|
|
|
self._38 = deferred;
|
2016-10-10 03:43:44 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
if (self._75 === 1) {
|
|
|
|
self._75 = 2;
|
|
|
|
self._38 = [self._38, deferred];
|
2016-10-10 03:43:44 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
self._38.push(deferred);
|
2016-10-10 03:43:44 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
handleResolved(self, deferred);
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleResolved(self, deferred) {
|
|
|
|
setImmediate(function() {
|
2017-12-10 21:51:33 +01:00
|
|
|
var cb = self._83 === 1 ? deferred.onFulfilled : deferred.onRejected;
|
2016-10-10 03:43:44 +02:00
|
|
|
if (cb === null) {
|
2017-12-10 21:51:33 +01:00
|
|
|
if (self._83 === 1) {
|
|
|
|
resolve(deferred.promise, self._18);
|
2016-10-10 03:43:44 +02:00
|
|
|
} else {
|
2017-12-10 21:51:33 +01:00
|
|
|
reject(deferred.promise, self._18);
|
2016-10-10 03:43:44 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
var ret = tryCallOne(cb, self._18);
|
2016-10-10 03:43:44 +02:00
|
|
|
if (ret === IS_ERROR) {
|
|
|
|
reject(deferred.promise, LAST_ERROR);
|
|
|
|
} else {
|
|
|
|
resolve(deferred.promise, ret);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
function resolve(self, newValue) {
|
|
|
|
// Promise Resolution Procedure: https://github.com/promises-aplus/promises-spec#the-promise-resolution-procedure
|
|
|
|
if (newValue === self) {
|
|
|
|
return reject(
|
|
|
|
self,
|
|
|
|
new TypeError('A promise cannot be resolved with itself.')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
newValue &&
|
|
|
|
(typeof newValue === 'object' || typeof newValue === 'function')
|
|
|
|
) {
|
|
|
|
var then = getThen(newValue);
|
|
|
|
if (then === IS_ERROR) {
|
|
|
|
return reject(self, LAST_ERROR);
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
then === self.then &&
|
|
|
|
newValue instanceof Promise
|
|
|
|
) {
|
2017-12-10 21:51:33 +01:00
|
|
|
self._83 = 3;
|
|
|
|
self._18 = newValue;
|
2016-10-10 03:43:44 +02:00
|
|
|
finale(self);
|
|
|
|
return;
|
|
|
|
} else if (typeof then === 'function') {
|
|
|
|
doResolve(then.bind(newValue), self);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
self._83 = 1;
|
|
|
|
self._18 = newValue;
|
2016-10-10 03:43:44 +02:00
|
|
|
finale(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
function reject(self, newValue) {
|
2017-12-10 21:51:33 +01:00
|
|
|
self._83 = 2;
|
|
|
|
self._18 = newValue;
|
|
|
|
if (Promise._71) {
|
|
|
|
Promise._71(self, newValue);
|
2016-10-10 03:43:44 +02:00
|
|
|
}
|
|
|
|
finale(self);
|
|
|
|
}
|
|
|
|
function finale(self) {
|
2017-12-10 21:51:33 +01:00
|
|
|
if (self._75 === 1) {
|
|
|
|
handle(self, self._38);
|
|
|
|
self._38 = null;
|
2016-10-10 03:43:44 +02:00
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
if (self._75 === 2) {
|
|
|
|
for (var i = 0; i < self._38.length; i++) {
|
|
|
|
handle(self, self._38[i]);
|
2016-10-10 03:43:44 +02:00
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
self._38 = null;
|
2016-10-10 03:43:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function Handler(onFulfilled, onRejected, promise){
|
|
|
|
this.onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : null;
|
|
|
|
this.onRejected = typeof onRejected === 'function' ? onRejected : null;
|
|
|
|
this.promise = promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Take a potentially misbehaving resolver function and make sure
|
|
|
|
* onFulfilled and onRejected are only called once.
|
|
|
|
*
|
|
|
|
* Makes no guarantees about asynchrony.
|
|
|
|
*/
|
|
|
|
function doResolve(fn, promise) {
|
|
|
|
var done = false;
|
|
|
|
var res = tryCallTwo(fn, function (value) {
|
|
|
|
if (done) return;
|
|
|
|
done = true;
|
|
|
|
resolve(promise, value);
|
|
|
|
}, function (reason) {
|
|
|
|
if (done) return;
|
|
|
|
done = true;
|
|
|
|
reject(promise, reason);
|
2017-08-14 05:01:11 +02:00
|
|
|
});
|
2016-10-10 03:43:44 +02:00
|
|
|
if (!done && res === IS_ERROR) {
|
|
|
|
done = true;
|
|
|
|
reject(promise, LAST_ERROR);
|
|
|
|
}
|
|
|
|
}
|