Would you like to clone this notebook?

When you clone a notebook you are able to make changes without affecting the original notebook.

Cancel

tiny-async-pool-es6

node v10.24.1
version: 1.0.0
endpointsharetweet
const results = []; const timeout = i => new Promise(resolve => setTimeout(() => { results.push(i); resolve(); }, i) ).then(() => { return i }); const urls = [100, 500, 300, 200] function asyncPool(poolLimit, array, iteratorFn) { let i = 0; const ret = []; const executing = []; const enqueue = function() { if (i === array.length) { return Promise.resolve(); } const item = array[i++]; const p = Promise.resolve().then(() => iteratorFn(item, array)); ret.push(p); const e = p.then(() => executing.splice(executing.indexOf(e), 1)); executing.push(e); let r = Promise.resolve(); if (executing.length >= poolLimit) { r = Promise.race(executing); } return r.then(() => enqueue()); }; return enqueue().then(() => Promise.all(ret)); } (async function main() { await asyncPool(2, urls, timeout); console.log(results) })()
Loading…

no comments

    sign in to comment