es6-promise.js
6.13 KB
"use strict";function _typeof(t){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}!function(t,e){"object"===("undefined"==typeof exports?"undefined":_typeof(exports))&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(void 0).ES6Promise=e()}(0,function(){function a(t){return"function"==typeof t}var n=Array.isArray?Array.isArray:function(t){return"[object Array]"===Object.prototype.toString.call(t)},r=0,e=void 0,o=void 0,u=function(t,e){h[r]=t,h[r+1]=e,2===(r+=2)&&(o?o(p):m())};var t="undefined"!=typeof window?window:void 0,i=t||{},s=i.MutationObserver||i.WebKitMutationObserver,c="undefined"==typeof self&&"undefined"!=typeof process&&"[object process]"==={}.toString.call(process),f="undefined"!=typeof Uint8ClampedArray&&"undefined"!=typeof importScripts&&"undefined"!=typeof MessageChannel;function l(){var t=setTimeout;return function(){return t(p,1)}}var h=new Array(1e3);function p(){for(var t=0;t<r;t+=2){(0,h[t])(h[t+1]),h[t]=void 0,h[t+1]=void 0}r=0}var v,d,_,y,m=void 0;function b(t,e){var n=arguments,r=this,o=new this.constructor(A);void 0===o[g]&&N(o);var i,s=r._state;return s?(i=n[s-1],u(function(){return K(s,o,i,r._result)})):k(r,o,t,e),o}function w(t){if(t&&"object"===_typeof(t)&&t.constructor===this)return t;var e=new this(A);return P(e,t),e}m=c?function(){return process.nextTick(p)}:s?(d=0,_=new s(p),y=document.createTextNode(""),_.observe(y,{characterData:!0}),function(){y.data=d=++d%2}):f?((v=new MessageChannel).port1.onmessage=p,function(){return v.port2.postMessage(0)}):void 0===t&&"function"==typeof require?function(){try{var t=require("vertx");return void 0!==(e=t.runOnLoop||t.runOnContext)?function(){e(p)}:l()}catch(t){return l()}}():l();var g=Math.random().toString(36).substring(16);function A(){}var S=void 0,j=1,E=2,T=new F;function x(t){try{return t.then}catch(t){return T.error=t,T}}function M(t,e,n){var r,o,i,s;e.constructor===t.constructor&&n===b&&e.constructor.resolve===w?(i=t,(s=e)._state===j?O(i,s._result):s._state===E?Y(i,s._result):k(s,void 0,function(t){return P(i,t)},function(t){return Y(i,t)})):n===T?Y(t,T.error):void 0===n?O(t,e):a(n)?(r=e,o=n,u(function(e){var n=!1,t=function(t,e,n,r){try{t.call(e,n,r)}catch(t){return t}}(o,r,function(t){n||(n=!0,r!==t?P(e,t):O(e,t))},function(t){n||(n=!0,Y(e,t))},e._label);!n&&t&&(n=!0,Y(e,t))},t)):O(t,e)}function P(t,e){var n;t===e?Y(t,new TypeError("You cannot resolve a promise with itself")):"function"==typeof(n=e)||"object"===_typeof(n)&&null!==n?M(t,e,x(e)):O(t,e)}function C(t){t._onerror&&t._onerror(t._result),q(t)}function O(t,e){t._state===S&&(t._result=e,t._state=j,0!==t._subscribers.length&&u(q,t))}function Y(t,e){t._state===S&&(t._state=E,t._result=e,u(C,t))}function k(t,e,n,r){var o=t._subscribers,i=o.length;t._onerror=null,o[i]=e,o[i+j]=n,o[i+E]=r,0===i&&t._state&&u(q,t)}function q(t){var e=t._subscribers,n=t._state;if(0!==e.length){for(var r=void 0,o=void 0,i=t._result,s=0;s<e.length;s+=3)r=e[s],o=e[s+n],r?K(n,r,o,i):o(i);t._subscribers.length=0}}function F(){this.error=null}var D=new F;function K(t,e,n,r){var o=a(n),i=void 0,s=void 0,u=void 0,c=void 0;if(o){if((i=function(t,e){try{return t(e)}catch(t){return D.error=t,D}}(n,r))===D?(c=!0,s=i.error,i=null):u=!0,e===i)return void Y(e,new TypeError("A promises callback cannot return that same promise."))}else i=r,u=!0;e._state!==S||(o&&u?P(e,i):c?Y(e,s):t===j?O(e,i):t===E&&Y(e,i))}var L=0;function N(t){t[g]=L++,t._state=void 0,t._result=void 0,t._subscribers=[]}function U(t,e){this._instanceConstructor=t,this.promise=new t(A),this.promise[g]||N(this.promise),n(e)?(this._input=e,this.length=e.length,this._remaining=e.length,this._result=new Array(this.length),0===this.length?O(this.promise,this._result):(this.length=this.length||0,this._enumerate(),0===this._remaining&&O(this.promise,this._result))):Y(this.promise,new Error("Array Methods must be provided an Array"))}function W(t){this[g]=L++,this._result=this._state=void 0,this._subscribers=[],A!==t&&("function"!=typeof t&&function(){throw new TypeError("You must pass a resolver function as the first argument to the promise constructor")}(),this instanceof W?function(e,t){try{t(function(t){P(e,t)},function(t){Y(e,t)})}catch(t){Y(e,t)}}(this,t):function(){throw new TypeError("Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function.")}())}return U.prototype._enumerate=function(){for(var t=this.length,e=this._input,n=0;this._state===S&&n<t;n++)this._eachEntry(e[n],n)},U.prototype._eachEntry=function(e,t){var n=this._instanceConstructor,r=n.resolve;if(r===w){var o=x(e);if(o===b&&e._state!==S)this._settledAt(e._state,t,e._result);else if("function"!=typeof o)this._remaining--,this._result[t]=e;else if(n===W){var i=new n(A);M(i,e,o),this._willSettleAt(i,t)}else this._willSettleAt(new n(function(t){return t(e)}),t)}else this._willSettleAt(r(e),t)},U.prototype._settledAt=function(t,e,n){var r=this.promise;r._state===S&&(this._remaining--,t===E?Y(r,n):this._result[e]=n),0===this._remaining&&O(r,this._result)},U.prototype._willSettleAt=function(t,e){var n=this;k(t,void 0,function(t){return n._settledAt(j,e,t)},function(t){return n._settledAt(E,e,t)})},W.all=function(t){return new U(this,t).promise},W.race=function(o){var i=this;return n(o)?new i(function(t,e){for(var n=o.length,r=0;r<n;r++)i.resolve(o[r]).then(t,e)}):new i(function(t,e){return e(new TypeError("You must pass an array to race."))})},W.resolve=w,W.reject=function(t){var e=new this(A);return Y(e,t),e},W._setScheduler=function(t){o=t},W._setAsap=function(t){u=t},W._asap=u,W.prototype={constructor:W,then:b,catch:function(t){return this.then(null,t)}},W.polyfill=function(){var t=void 0;if("undefined"!=typeof global)t=global;else if("undefined"!=typeof self)t=self;else try{t=Function("return this")()}catch(t){throw new Error("polyfill failed because global object is unavailable in this environment")}var e=t.Promise;if(e){var n=null;try{n=Object.prototype.toString.call(e.resolve())}catch(t){}if("[object Promise]"===n&&!e.cast)return}t.Promise=W},W.Promise=W});