Skip to content

Instantly share code, notes, and snippets.

@Nezteb
Last active March 27, 2023 21:29
Show Gist options
  • Save Nezteb/d33daed2ac283c6feda111f190e5f540 to your computer and use it in GitHub Desktop.
Save Nezteb/d33daed2ac283c6feda111f190e5f540 to your computer and use it in GitHub Desktop.
Say you have 100 promises worth of work but want to ensure only 5 happen at a time; you want to chunk the promises.
// Originally by @FizzyGalacticus
// https://github.com/FizzyGalacticus/chunky-promise
// Modified by me
function chunkList(originalList = [], chunkSize = 5) {
const listOfChunks = [];
for(let i = 0; i < originalList.length; i += chunkSize) {
const chunk = originalList.slice(i, i + chunkSize)
listOfChunks.push(chunk);
}
return listOfChunks;
};
function do_some_work(data) {
// Return promise, do not await it
return new Promise(resolve => {
setTimeout(() => {
console.log(`\t${data}`);
resolve();
}, 1000);
});
}
(async () => {
const moby_dick_first_paragraph = "Call me Ishmael. Some years ago - never mind how long precisely - having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen and regulating the circulation. Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meet; and especially whenever my hypos get such an upper hand of me, that it requires a strong moral principle to prevent me from deliberately stepping into the street, and methodically knocking people's hats off - then, I account it high time to get to sea as soon as I can. This is my substitute for pistol and ball. With a philosophical flourish Cato throws himself upon his sword; I quietly take to the ship. There is nothing surprising in this. If they but knew it, almost all men in their degree, some time or other, cherish very nearly the same feelings towards the ocean with me."
const data_to_chunk = moby_dick_first_paragraph.split(" ");
const chunks = chunkList(data_to_chunk, 10);
let promiseResults = [];
for(const [i, chunk] of chunks.entries()) {
console.log(`Chunk ${i}: (size ${chunk.length})`)
const chunkedPromises = await Promise.all(
chunk.map(
data => do_some_work(data)
)
)
promiseResults.push(...chunkedPromises);
}
promiseResults = promiseResults.flat();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment