zalgo-promise
Version:
Javascript module template.
2 lines • 6.41 kB
JavaScript
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("ZalgoPromise",[],t):"object"==typeof exports?exports.ZalgoPromise=t():e.ZalgoPromise=t()}("undefined"!=typeof self?self:this,(function(){return function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return{}.hasOwnProperty.call(e,t)},r.p="",r(r.s=0)}([function(e,t,r){"use strict";function n(e){try{if(!e)return!1;if("undefined"!=typeof Promise&&e instanceof Promise)return!0;if("undefined"!=typeof window&&"function"==typeof window.Window&&e instanceof window.Window)return!1;if("undefined"!=typeof window&&"function"==typeof window.constructor&&e instanceof window.constructor)return!1;var t={}.toString;if(t){var r=t.call(e);if("[object Window]"===r||"[object global]"===r||"[object DOMWindow]"===r)return!1}if("function"==typeof e.then)return!0}catch(e){return!1}return!1}r.r(t),r.d(t,"ZalgoPromise",(function(){return a}));var o,i=[],c=[],u=0;function s(){if(!u&&o){var e=o;o=null,e.resolve()}}function f(){u+=1}function l(){u-=1,s()}var a=function(){function e(e){var t=this;if(this.resolved=void 0,this.rejected=void 0,this.errorHandled=void 0,this.value=void 0,this.error=void 0,this.handlers=void 0,this.dispatching=void 0,this.stack=void 0,this.resolved=!1,this.rejected=!1,this.errorHandled=!1,this.handlers=[],e){var r,n,o=!1,i=!1,c=!1;f();try{e((function(e){c?t.resolve(e):(o=!0,r=e)}),(function(e){c?t.reject(e):(i=!0,n=e)}))}catch(e){return l(),void this.reject(e)}l(),c=!0,o?this.resolve(r):i&&this.reject(n)}}var t=e.prototype;return t.resolve=function(e){if(this.resolved||this.rejected)return this;if(n(e))throw new Error("Can not resolve promise with another promise");return this.resolved=!0,this.value=e,this.dispatch(),this},t.reject=function(e){var t=this;if(this.resolved||this.rejected)return this;if(n(e))throw new Error("Can not reject promise with another promise");if(!e){var r=e&&"function"==typeof e.toString?e.toString():{}.toString.call(e);e=new Error("Expected reject to be called with Error, got "+r)}return this.rejected=!0,this.error=e,this.errorHandled||setTimeout((function(){t.errorHandled||function(e,t){if(-1===i.indexOf(e)){i.push(e),setTimeout((function(){throw e}),1);for(var r=0;r<c.length;r++)c[r](e,t)}}(e,t)}),1),this.dispatch(),this},t.asyncReject=function(e){return this.errorHandled=!0,this.reject(e),this},t.dispatch=function(){var t=this.resolved,r=this.rejected,o=this.handlers;if(!this.dispatching&&(t||r)){this.dispatching=!0,f();for(var i=function(e,t){return e.then((function(e){t.resolve(e)}),(function(e){t.reject(e)}))},c=0;c<o.length;c++){var u=o[c],s=u.onSuccess,a=u.onError,h=u.promise,d=void 0;if(t)try{d=s?s(this.value):this.value}catch(e){h.reject(e);continue}else if(r){if(!a){h.reject(this.error);continue}try{d=a(this.error)}catch(e){h.reject(e);continue}}if(d instanceof e&&(d.resolved||d.rejected)){var v=d;v.resolved?h.resolve(v.value):h.reject(v.error),v.errorHandled=!0}else n(d)?d instanceof e&&(d.resolved||d.rejected)?d.resolved?h.resolve(d.value):h.reject(d.error):i(d,h):h.resolve(d)}o.length=0,this.dispatching=!1,l()}},t.then=function(t,r){if(t&&"function"!=typeof t&&!t.call)throw new Error("Promise.then expected a function for success handler");if(r&&"function"!=typeof r&&!r.call)throw new Error("Promise.then expected a function for error handler");var n=new e;return this.handlers.push({promise:n,onSuccess:t,onError:r}),this.errorHandled=!0,this.dispatch(),n},t.catch=function(e){return this.then(void 0,e)},t.finally=function(t){if(t&&"function"!=typeof t&&!t.call)throw new Error("Promise.finally expected a function");return this.then((function(r){return e.try(t).then((function(){return r}))}),(function(r){return e.try(t).then((function(){throw r}))}))},t.timeout=function(e,t){var r=this;if(this.resolved||this.rejected)return this;var n=setTimeout((function(){r.resolved||r.rejected||r.reject(t||new Error("Promise timed out after "+e+"ms"))}),e);return this.then((function(e){return clearTimeout(n),e}))},t.toPromise=function(){if("undefined"==typeof Promise)throw new TypeError("Could not find Promise");return Promise.resolve(this)},t.lazy=function(){return this.errorHandled=!0,this},e.resolve=function(t){return t instanceof e?t:n(t)?new e((function(e,r){return t.then(e,r)})):(new e).resolve(t)},e.reject=function(t){return(new e).reject(t)},e.asyncReject=function(t){return(new e).asyncReject(t)},e.all=function(t){var r=new e,o=t.length,i=[].slice();if(!o)return r.resolve(i),r;for(var c=function(e,t,n){return t.then((function(t){i[e]=t,0==(o-=1)&&r.resolve(i)}),(function(e){n.reject(e)}))},u=0;u<t.length;u++){var s=t[u];if(s instanceof e){if(s.resolved){i[u]=s.value,o-=1;continue}}else if(!n(s)){i[u]=s,o-=1;continue}c(u,e.resolve(s),r)}return 0===o&&r.resolve(i),r},e.hash=function(t){var r={},o=[],i=function(e){if(t.hasOwnProperty(e)){var i=t[e];n(i)?o.push(i.then((function(t){r[e]=t}))):r[e]=i}};for(var c in t)i(c);return e.all(o).then((function(){return r}))},e.map=function(t,r){return e.all(t.map(r))},e.onPossiblyUnhandledException=function(e){return function(e){return c.push(e),{cancel:function(){c.splice(c.indexOf(e),1)}}}(e)},e.try=function(t,r,n){if(t&&"function"!=typeof t&&!t.call)throw new Error("Promise.try expected a function");var o;f();try{o=t.apply(r,n||[])}catch(t){return l(),e.reject(t)}return l(),e.resolve(o)},e.delay=function(t){return new e((function(e){setTimeout(e,t)}))},e.isPromise=function(t){return!!(t&&t instanceof e)||n(t)},e.flush=function(){return t=o=o||new e,s(),t;var t},e}()}])}));
//# sourceMappingURL=zalgo-promise.js.map