-
Notifications
You must be signed in to change notification settings - Fork 3
/
promise-min.js
1 lines (1 loc) · 3.22 KB
/
promise-min.js
1
"use strict";function Promise(e){if(!(this instanceof Promise))throw new TypeError("Constructor Promise requires `new`");if(!isFunction(e))throw new TypeError("Must pass resolver function");this._state=PendingPromise,this._value=[],this._isChainEnd=!0,doResolve(this,adopter(this,FulfilledPromise),adopter(this,RejectedPromise),{then:e})}function FulfilledPromise(e,n,t,r){return n?(r||(r=new Deferred(this.constructor)),defer(tryCatchDeferred(r,n,e)),r.promise):(deferredAdopt(r,FulfilledPromise,e),this)}function RejectedPromise(e,n,t,r){return t?(r||(r=new Deferred(this.constructor)),defer(tryCatchDeferred(r,t,e)),r.promise):(deferredAdopt(r,RejectedPromise,e),this)}function PendingPromise(e,n,t,r){if(!r){if(!n&&!t)return this;r=new Deferred(this.constructor)}return e.push({deferred:r,onFulfilled:n||r.resolve,onRejected:t||r.reject}),r.promise}function Deferred(e){var n=this;return this.promise=new e(function(e,t){n.resolve=e,n.reject=t}),n}function adopt(e,n,t,r){var o=e._value;e._state=n,e._value=t,r&&n===PendingPromise&&r._state(t,void 0,void 0,{promise:e,resolve:void 0,reject:void 0});for(var i=0;i<o.length;i++){var s=o[i];e._state(t,s.onFulfilled,s.onRejected,s.deferred)}o.length=0,n===RejectedPromise&&e._isChainEnd&&setTimeout(function(){e._isChainEnd&&onPossiblyUnhandledRejection(t,e)},0)}function adopter(e,n){return function(t){adopt(e,n,t)}}function deferredAdopt(e,n,t){if(e){var r=e.promise;r._state=n,r._value=t}}function noop(){}function isFunction(e){return"function"==typeof e}function isObject(e){return e===Object(e)}function each(e,n){for(var t=0;t<e.length;t++)n(e[t],t)}function tryCatchDeferred(e,n,t){var r=e.promise,o=e.resolve,i=e.reject;return function(){try{var e=n(t);doResolve(r,o,i,e,e)}catch(e){i(e)}}}function doResolve(e,n,t,r,o){var i,s,c=t;try{if(r===e)throw new TypeError("Cannot fulfill promise with itself");var u=isObject(r);u&&r instanceof e.constructor?adopt(e,r._state,r._value,r):u&&(i=r.then)&&isFunction(i)?(s=function(r){s=c=noop,doResolve(e,n,t,r,r)},c=function(e){s=c=noop,t(e)},i.call(o,function(e){s(e)},function(e){c(e)})):n(r)}catch(e){c(e)}}Promise.prototype.then=function(e,n){return e=isFunction(e)?e:void 0,n=isFunction(n)?n:void 0,(e||n)&&(this._isChainEnd=!1),this._state(this._value,e,n)},Promise.prototype.catch=function(e){return this.then(void 0,e)},Promise.resolve=function(e){var n=this;return isObject(e)&&e instanceof this?e:new n(function(n){n(e)})},Promise.reject=function(e){return new this(function(n,t){t(e)})},Promise.all=function(e){var n=this;return new n(function(t,r){var o=e.length,i=new Array(o);if(0===o)return t(i);each(e,function(e,s){n.resolve(e).then(function(e){i[s]=e,0==--o&&t(i)},r)})})},Promise.race=function(e){var n=this;return new n(function(t,r){for(var o=0;o<e.length;o++)n.resolve(e[o]).then(t,r)})};var onPossiblyUnhandledRejection=function(e,n){throw e};Promise._overrideUnhandledExceptionHandler=function(e){onPossiblyUnhandledRejection=e};var defer=function(){function e(){for(var e=0;e<r;e++){var n=t[e];t[e]=null,n()}r=0}var n;"undefined"!=typeof window&&window.postMessage?(window.addEventListener("message",e),n=function(){window.postMessage("macro-task","*")}):n=function(){setTimeout(e,0)};var t=new Array(16),r=0;return function(e){0===r&&n(),t[r++]=e}}();module.exports=Promise;