UNPKG

@yuanjili/js-es-c

Version:

js工具包

1 lines 2.74 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a},_createClass=function(){function a(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(a,c.key,c)}return function(b,c,d){return c&&a(b.prototype,c),d&&a(b,d),b}}();exports.asyncFunctionWithConcurrencyLimit=asyncFunctionWithConcurrencyLimit;function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}var MyPromise=exports.MyPromise=function(){function a(b){var c=this;_classCallCheck(this,a),this.state="pending",this.value=void 0,this.reason=void 0,this.onFulfilledCallbacks=[],this.onRejectedCallbacks=[];var d=function(a){"pending"===c.state&&(c.state="fulfilled",c.value=a,c.onFulfilledCallbacks.forEach(function(a){return a(c.value)}))},e=function(a){"pending"===c.state&&(c.state="rejected",c.reason=a,c.onRejectedCallbacks.forEach(function(a){return a(c.reason)}))};try{b(d,e)}catch(a){e(a)}}return _createClass(a,[{key:"then",value:function(b,c){var d=this;b="function"==typeof b?b:function(a){return a},c="function"==typeof c?c:function(a){throw a};var e=new a(function(a,f){"fulfilled"===d.state?setTimeout(function(){try{var c=b(d.value);resolvePromise(e,c,a,f)}catch(a){f(a)}},0):"rejected"===d.state?setTimeout(function(){try{var b=c(d.reason);resolvePromise(e,b,a,f)}catch(a){f(a)}},0):(d.onFulfilledCallbacks.push(function(c){setTimeout(function(){try{var d=b(c);resolvePromise(e,d,a,f)}catch(a){f(a)}},0)}),d.onRejectedCallbacks.push(function(b){setTimeout(function(){try{var d=c(b);resolvePromise(e,d,a,f)}catch(a){f(a)}},0)}))});return e}},{key:"catch",value:function(a){return this.then(null,a)}}]),a}();function resolvePromise(a,b,c,d){if(a===b)return d(new TypeError("Chaining cycle detected for promise"));var f=!1;if(b instanceof MyPromise)b.then(function(b){resolvePromise(a,b,c,d)},d);else if(null!==b&&("object"===("undefined"==typeof b?"undefined":_typeof(b))||"function"==typeof b))try{var g=b.then;"function"==typeof g?g.call(b,function(b){f||(f=!0,resolvePromise(a,b,c,d))},function(a){f||(f=!0,d(a))}):c(b)}catch(a){if(f)return;f=!0,d(a)}else c(b)}function asyncFunctionWithConcurrencyLimit(a,b){function c(){if(f<a.length&&d<b){d++;var g=f;f++;var h=a[g];h().then(function(a){e[g]=a,d--,c()}).catch(function(a){e[g]=a,d--,c()})}}for(var d=0,e=[],f=0,g=0;g<b&&g<a.length;g++)c();return new Promise(function(b){var c=setInterval(function(){e.length===a.length&&(clearInterval(c),b(e))},100)})}