UNPKG

promise-parallel-throttle

Version:
3 lines (2 loc) 1.75 kB
var e={maxInProgress:5,failFast:!1,nextCheck:function(e,n){return Promise.resolve(e.amountStarted<n.length)},ignoreIsFunctionCheck:!1};function n(n,t){return new Promise((function(s,o){var r=Object.assign({},e,t),a={lastCompletedIndex:-1,amountDone:0,amountStarted:0,amountResolved:0,amountRejected:0,amountNextCheckFalsey:0,rejectedIndexes:[],resolvedIndexes:[],nextCheckFalseyIndexes:[],taskResults:[]};if(0===n.length)return s(a);for(var u=!1,c=0,i=function(e,n){if(a.taskResults[n]=e,a.rejectedIndexes.push(n),a.amountRejected++,!0===r.failFast)return a.lastCompletedIndex=n,u=!0,o(a);l(n)},l=function(e){if(!0!==u){if(a.amountDone++,a.lastCompletedIndex=e,"function"==typeof r.progressCallback&&r.progressCallback(a),a.amountDone===n.length)return s(a);c<n.length&&f(c++)}},f=function(e){r.nextCheck(a,n).then((function(t){!0===t?function(e){if(a.amountStarted++,"function"==typeof n[e])try{n[e]().then((function(n){a.taskResults[e]=n,a.resolvedIndexes.push(e),a.amountResolved++,l(e)}),(function(n){i(n,e)}))}catch(n){i(n,e)}else{if(!0!==r.ignoreIsFunctionCheck)return u=!0,o(new Error("tasks["+e+"]: "+n[e]+", is supposed to be of type function"));a.taskResults[e]=n[e],a.resolvedIndexes.push(e),a.amountResolved++,l(e)}}(e):(a.amountNextCheckFalsey++,a.nextCheckFalseyIndexes.push(e),l(e))}),o)},d=0;d<Math.min(r.maxInProgress,n.length);d++)f(c++)}))}function t(e,t){return new Promise((function(s,o){n(e,t).then((function(e){s(e.taskResults)}),(function(e){e instanceof Error?o(e):o(e.taskResults[e.rejectedIndexes[0]])}))}))}function s(e,n){return t(e,Object.assign({},{maxInProgress:1,failFast:!0},n))}function o(e,n){return t(e,Object.assign({},{failFast:!0},n))}export{o as all,n as raw,s as sync}; //# sourceMappingURL=throttle.mjs.map