UNPKG

es6-promise-shim

Version:

A lightweight implementation of Promise in pure ES5 code for older JS engines

1 lines 5.18 kB
!function(e,n){"use strict";"object"==typeof module&&"object"==typeof module.exports?module.exports=n(e):n(e)}("undefined"!=typeof window?window:global,function(e){"use strict";var n=Object.defineProperty,t=Object.defineProperties,r=Array.prototype.slice,i=(Array.prototype.forEach,Array.isArray),o=Math.floor,u=Math.abs,c="object"==typeof e.ES6?e.ES6:e.ES6={},f=Math.max,l=(Math.min,function(e){return"function"==typeof e}),s=function(e){return null!==e&&("object"==typeof e||"function"==typeof e)},a=function(e,n){if(!l(e))throw new TypeError(e+" is not a function");var t=r.call(arguments,2);setTimeout(function(){e.apply(n,t)})},h=function e(n){if(!(this instanceof e)||T(this))throw new TypeError(String(this)+" is not a promise");if(!l(n))throw new TypeError("Promise resolver "+String(n)+" is not a function");b(this);try{n(function(e){this._resolve(e)}.bind(this),function(e){this._reject(e)}.bind(this))}catch(e){this._reject(e)}};h.resolve=function(e){return T(e)?e:new h(function(n,t){v(e)?a(function(){try{e.then(n,t)}catch(e){t(e)}}):n(e)})},h.reject=function(e){return new h(function(n,t){t(e)})},h.race=function(e){if(void 0===e||null===e)return new h(function(e,n){a(n,void 0,TypeError("First argument of Promise.race can not be undefined or null"))});var n,r=!1;return n=Number(e.length),n=n!==n?0:n,n=(n<0?-1:1)*o(u(n)),n=f(n,0),new h(function(i,o){var u,c=0;for(u=function(e){var n,u;T(e)?g(e)?r||(r=!0,a(function(){i(e._value)})):m(e)?r||(r=!0,a(function(){o(e._reason)})):w(e)&&(n=e._resolve,u=e._reject,t(e,{_resolve:{value:function(e){n(e),r||(r=!0,i(e))}.bind(e)},_reject:{value:function(e){u(e),r||(r=!0,o(e))}.bind(e)}})):v(e)?a(function(){try{e.then(function(e){r||(r=!0,i(e))},function(e){r||(r=!0,o(e))})}catch(e){o(e)}}):r||(r=!0,a(function(){i(e)}))};c<n;++c)u(e[c])})},h.all=function(e){if(void 0===e||null===e)return new h(function(e,n){a(n,void 0,TypeError("First argument of Promise.all can not be undefined or null"))});var n,r,i=0;return n=Number(e.length),n=n!==n?0:n,n=(n<0?-1:1)*o(u(n)),n=f(n,0),r=new Array(n),new h(function(o,u){var c,f=0;if(0===n)o(r);else for(c=function(e,c){var f,l;T(e)?g(e)?(r[c]=e._value,++i===n&&a(function(){o(r)})):m(e)?a(function(){u(e._reason)}):w(e)&&(f=e._resolve,l=e._reject,t(e,{_resolve:{value:function(e){f(e),r[c]=e,++i===n&&o(r)}.bind(e)},_reject:{value:function(e){l(e),u(e)}.bind(e)}})):v(e)?a(function(){try{e.then(function(e){r[c]=e,++i===n&&o(r)},function(e){u(e)})}catch(e){u(e)}}):(r[c]=e,++i===n&&a(function(){o(r)}))};f<n;++f)c(e[f],f)})},h.prototype.then=function(e,n){if(!T(this))throw new TypeError(this+" is not a promise");e=l(e)?e:d,n=l(n)?n:p;var t,r,i=new h(function(e,n){});return t=function(n){var t;try{t=e(n),_(t,i)}catch(e){i._reject(e)}},r=function(e){var t;try{t=n(e),_(t,i)}catch(e){i._reject(e)}},w(this)?(this._onFulfilled.push(t),this._onRejected.push(r)):g(this)?a(t,void 0,this._value):m(this)&&a(r,void 0,this._reason),i};var _=function(e,n){var r,i;T(e)?g(e)?n._resolve(e._value):m(e)?n._reject(e._reason):w(e)&&(r=e._resolve,i=e._reject,t(e,{_resolve:{value:function(e){r(e),n._resolve(e)}.bind(e)},_reject:{value:function(e){i(e),n._reject(e)}.bind(e)}})):v(e)?a(function(){try{e.then(function(e){this._resolve(e)}.bind(n),function(e){this._reject(e)}.bind(n))}catch(e){n._reject(e)}}):n._resolve(e)};h.prototype.catch=function(e){if(!l(this.then))throw new TypeError("(var).then is not a function");return this.then(void 0,e)},h.prototype.toString=function(){if(!T(this))throw new TypeError(this+" is not a promise");switch(this._state){case"pending":return"Promise { <pending> }";case"fulfilled":return"Promise { "+this._value+" }";case"rejected":return"Promise { <rejected> "+this._reason+" }"}};var v=function(e){return s(e)&&l(e.then)},d=function(e){return h.resolve(e)},p=function(e){return h.reject(e)},j=function(e){E(this)||(t(this,{_state:{value:"fulfilled"},_value:{value:e}}),this._onFulfilled.length>0&&a(function(e){this._onFulfilled.forEach(function(n){n(e)}),this._onFulfilled.length=0,this._onRejected.length=0},this,e))},y=function(e){E(this)||(t(this,{_state:{value:"rejected"},_reason:{value:e}}),this._onRejected.length>0&&a(function(e){this._onRejected.forEach(function(n){n(e)}),this._onFulfilled.length=0,this._onRejected.length=0},this,e))},b=function(e){t(e,{_isPromise:{value:!0},_onFulfilled:{value:[]},_onRejected:{value:[]},_resolve:{value:j.bind(e),configurable:!0},_reject:{value:y.bind(e),configurable:!0},_state:{value:"pending",configurable:!0},_value:{value:void 0,configurable:!0},_reason:{value:void 0,configurable:!0}})},w=function(e){return"pending"===e._state},g=function(e){return"fulfilled"===e._state},m=function(e){return"rejected"===e._state},E=function(e){return"fulfilled"===e._state||"rejected"===e._state},P=function(e){return-1!==["pending","fulfilled","rejected"].indexOf(String(e))},F=function(e){return!0===e._isPromise&&i(e._onFulfilled)&&i(e._onRejected)&&l(e._resolve)&&l(e._reject)&&P(e._state)&&e.hasOwnProperty("_value")&&e.hasOwnProperty("_reason")},T=function(e){return e instanceof h&&F(e)};return t(c,{isPromise:{value:T,writable:!0,configurable:!0}}),n(e,"Promise",{value:h,writable:!0,configurable:!0}),c});