Skip to content

Instantly share code, notes, and snippets.

@ChillyBwoy
Created August 4, 2023 07:57
Show Gist options
  • Save ChillyBwoy/a937d8021e2205fbda6b3b60db41e307 to your computer and use it in GitHub Desktop.
Save ChillyBwoy/a937d8021e2205fbda6b3b60db41e307 to your computer and use it in GitHub Desktop.
Promises queue
/**
- you have a list of 1000 items
- you have an async function `process(item)`
- you need to process all items
- it needs to be done concurrently, but not more than 25 at a time
- collect items with errors
what's the cleanest way to do this?
libraries allowed
*/
function solution(items, limit, process) {
return new Promise((resolve) => {
let count = 0;
let slots = 0;
let resolved = 0;
const result = new Array(items.length);
const next = () => {
if (resolved >= items.length) {
return resolve(result);
}
if (count >= items.length || slots >= limit) {
return;
}
const idx = count;
process(items[idx])
.then((data) => {
result[idx] = data;
slots -= 1;
resolved += 1;
})
.catch((err) => {
result[idx] = err;
slots -= 1;
resolved += 1;
})
.then(next);
count += 1;
slots += 1;
next();
};
next();
});
}
const items = new Array(1000).fill(0).map(
(_, i) => () =>
new Promise((resolve) => {
const time = Math.random() * 1000;
setTimeout(() => resolve([i, time]), time);
})
);
const process = (item) => item();
solution(items, 25, process).then((result) => {
for (const r of result) {
console.log(r);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment