Skip to content

Instantly share code, notes, and snippets.

@Xedii
Created December 28, 2018 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xedii/9c6f6ea4171a171f42d910a85d936388 to your computer and use it in GitHub Desktop.
Save Xedii/9c6f6ea4171a171f42d910a85d936388 to your computer and use it in GitHub Desktop.
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