1 line
3.3 KiB
JavaScript
1 line
3.3 KiB
JavaScript
function g(t){var n=this.constructor;return this.then(function(e){return n.resolve(t()).then(function(){return e})},function(e){return n.resolve(t()).then(function(){return n.reject(e)})})}function w(t){var n=this;return new n(function(e,r){if(!(t&&typeof t.length!="undefined"))return r(new TypeError(typeof t+" "+t+" is not iterable(cannot read property Symbol(Symbol.iterator))"));var i=Array.prototype.slice.call(t);if(i.length===0)return e([]);var h=i.length;function f(u,c){if(c&&(typeof c=="object"||typeof c=="function")){var s=c.then;if(typeof s=="function"){s.call(c,function(y){f(u,y)},function(y){i[u]={status:"rejected",reason:y},--h===0&&e(i)});return}}i[u]={status:"fulfilled",value:c},--h===0&&e(i)}for(var a=0;a<i.length;a++)f(a,i[a])})}var b=setTimeout;function _(t){return Boolean(t&&typeof t.length!="undefined")}function j(){}function F(t,n){return function(){t.apply(n,arguments)}}function o(t){if(!(this instanceof o))throw new TypeError("Promises must be constructed via new");if(typeof t!="function")throw new TypeError("not a function");this._state=0,this._handled=!1,this._value=void 0,this._deferreds=[],v(t,this)}function m(t,n){for(;t._state===3;)t=t._value;if(t._state===0){t._deferreds.push(n);return}t._handled=!0,o._immediateFn(function(){var e=t._state===1?n.onFulfilled:n.onRejected;if(e===null){(t._state===1?d:p)(n.promise,t._value);return}var r;try{r=e(t._value)}catch(i){p(n.promise,i);return}d(n.promise,r)})}function d(t,n){try{if(n===t)throw new TypeError("A promise cannot be resolved with itself.");if(n&&(typeof n=="object"||typeof n=="function")){var e=n.then;if(n instanceof o){t._state=3,t._value=n,l(t);return}else if(typeof e=="function"){v(F(e,n),t);return}}t._state=1,t._value=n,l(t)}catch(r){p(t,r)}}function p(t,n){t._state=2,t._value=n,l(t)}function l(t){t._state===2&&t._deferreds.length===0&&o._immediateFn(function(){t._handled||o._unhandledRejectionFn(t._value)});for(var n=0,e=t._deferreds.length;n<e;n++)m(t,t._deferreds[n]);t._deferreds=null}function P(t,n,e){this.onFulfilled=typeof t=="function"?t:null,this.onRejected=typeof n=="function"?n:null,this.promise=e}function v(t,n){var e=!1;try{t(function(r){e||(e=!0,d(n,r))},function(r){e||(e=!0,p(n,r))})}catch(r){if(e)return;e=!0,p(n,r)}}o.prototype.catch=function(t){return this.then(null,t)};o.prototype.then=function(t,n){var e=new this.constructor(j);return m(this,new P(t,n,e)),e};o.prototype.finally=g;o.all=function(t){return new o(function(n,e){if(!_(t))return e(new TypeError("Promise.all accepts an array"));var r=Array.prototype.slice.call(t);if(r.length===0)return n([]);var i=r.length;function h(a,u){try{if(u&&(typeof u=="object"||typeof u=="function")){var c=u.then;if(typeof c=="function"){c.call(u,function(s){h(a,s)},e);return}}r[a]=u,--i===0&&n(r)}catch(s){e(s)}}for(var f=0;f<r.length;f++)h(f,r[f])})};o.allSettled=w;o.resolve=function(t){return t&&typeof t=="object"&&t.constructor===o?t:new o(function(n){n(t)})};o.reject=function(t){return new o(function(n,e){e(t)})};o.race=function(t){return new o(function(n,e){if(!_(t))return e(new TypeError("Promise.race accepts an array"));for(var r=0,i=t.length;r<i;r++)o.resolve(t[r]).then(n,e)})};o._immediateFn=typeof setImmediate=="function"&&function(t){setImmediate(t)}||function(t){b(t,0)};o._unhandledRejectionFn=function(n){typeof console!="undefined"&&console&&console.warn("Possible Unhandled Promise Rejection:",n)};export{o as P};
|