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