Skip to content

Instantly share code, notes, and snippets.

@Xedii Xedii/mapLimit.js Secret
Created Dec 28, 2018

Embed
What would you like to do?
const mapLimit = async (arr, perChunk) => {
const promiseArr = arr.reduce((all,one,i) => {
const ch = Math.floor(i/perChunk);
all[ch] = [].concat((all[ch]||[]),one);
return all
}, [])
for (const promise of promiseArr) {
await Promise.all(promise.map(func => func()))
console.log('done!');
}
return 'Success!!'
}
const promiseArr = [request, request, request, request, request, request, request, request, request, request, request, request]
(async () => {
try {
const limit = 3;
await mapLimit(promiseArr, limit);
} catch (err) {
console.log(err)
}
})();
// or …
// ;(() => {
// mapLimit(datadad, 3).then(console.log).catch(console.error)
// })()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.