1 line
16 KiB
JSON
1 line
16 KiB
JSON
|
{"ast":null,"code":"'use strict';\n\nvar asap = require('asap/raw');\n\nfunction noop() {} // States:\n//\n// 0 - pending\n// 1 - fulfilled with _value\n// 2 - rejected with _value\n// 3 - adopted the state of another promise, _value\n//\n// once the state is no longer pending (0) it is immutable\n// All `_` prefixed properties will be reduced to `_{random number}`\n// at build time to obfuscate them and discourage their use.\n// We don't use symbols or Object.defineProperty to fully hide them\n// because the performance isn't good enough.\n// to avoid using try/catch inside critical functions, we\n// extract them to here.\n\n\nvar LAST_ERROR = null;\nvar IS_ERROR = {};\n\nfunction getThen(obj) {\n try {\n return obj.then;\n } catch (ex) {\n LAST_ERROR = ex;\n return IS_ERROR;\n }\n}\n\nfunction tryCallOne(fn, a) {\n try {\n return fn(a);\n } catch (ex) {\n LAST_ERROR = ex;\n return IS_ERROR;\n }\n}\n\nfunction tryCallTwo(fn, a, b) {\n try {\n fn(a, b);\n } catch (ex) {\n LAST_ERROR = ex;\n return IS_ERROR;\n }\n}\n\nmodule.exports = Promise;\n\nfunction Promise(fn) {\n if (typeof this !== 'object') {\n throw new TypeError('Promises must be constructed via new');\n }\n\n if (typeof fn !== 'function') {\n throw new TypeError('Promise constructor\\'s argument is not a function');\n }\n\n this._U = 0;\n this._V = 0;\n this._W = null;\n this._X = null;\n if (fn === noop) return;\n doResolve(fn, this);\n}\n\nPromise._Y = null;\nPromise._Z = null;\nPromise._0 = noop;\n\nPromise.prototype.then = function (onFulfilled, onRejected) {\n if (this.constructor !== Promise) {\n return safeThen(this, onFulfilled, onRejected);\n }\n\n var res = new Promise(noop);\n handle(this, new Handler(onFulfilled, onRejected, res));\n return res;\n};\n\nfunction safeThen(self, onFulfilled, onRejected) {\n return new self.constructor(function (resolve, reject) {\n var res = new Promise(noop);\n res.then(resolve, reject);\n handle(self, new Handler(onFulfilled, onRejected, res));\n });\n}\n\nfunction handle(self, deferred) {\n while (self._V === 3) {\n self = self._W;\n }\n\n if (Promise._Y) {\n Promise._Y(self);\n }\n\n if (self._V === 0) {\n if (self._U === 0) {\n self._U = 1;\n self._X = deferred;\n return;\n }\n\n if (self._U === 1) {\n self._U = 2;\n self._X = [self._X, deferred];\n return;\n }\n\n self._X.push(deferred);\n\n return;\n }\n\n handleResolved(self, deferred);\n}\n\nfunction handleResolved(self, deferred) {\n asap(function () {\n var cb = self._V === 1 ? deferred.onFulfilled : deferred.onRejected;\n\n if (cb === null) {\n if (self._V === 1) {\n resolve(deferred.promise, self._W);\n } else {\n reject(deferred.promise, self._W);\n }\n\n return;\n }\n\n var ret = tryCallOne(cb, self._W);\n\n if (ret === IS_ERROR) {\n reject(deferred.promise, LAST_ERROR);\n } else {\n resolve(deferred.promise, ret);\n }\n });\n}\n\nfunction resolve(self, newValue) {\n // Promise Resolution Procedure: https://github.com/promises-aplus/promises-spec#the-promise-resolution-procedure\n if (newValue === self) {\n return reject(self, new TypeError('A promise cannot be resolved with itself.'));\n }\n\n if (newValue && (typeof newValue === 'object' || typeof newValue === 'function')) {\n var then = getThen(newValue);\n\n if (then === IS_ERROR) {\n return reject(self, LAST_ERROR);\n }\n\n if (then === self.then && newValue instanceof Promise) {\n self._V = 3;\n self._W = newValue;\n finale(self);\n return;\n } else if (typeof then === 'function') {\n doResolve(then.bind(newValue), self);\n return;\n }\n }\n\n self._V = 1;\n self._W = newValue;\n finale(self);\n}\n\nfunction reject(self, newValue) {\n self._V = 2;\n self._W = newValue;\n\n if (Promise._Z) {\n Promise._Z(self, newValue);\n }\n\n finale(self);\n}\n\nfunction finale(self) {\n if (self._U === 1) {\n handle(self, self._X);\n se
|