Skip to content

Instantly share code, notes, and snippets.

@smigniot
Created February 4, 2024 07:30
Show Gist options
  • Select an option

  • Save smigniot/551c972d663410fc52c6c928adc17bb0 to your computer and use it in GitHub Desktop.

Select an option

Save smigniot/551c972d663410fc52c6c928adc17bb0 to your computer and use it in GitHub Desktop.
async function run(elements) {
// ============
// your code here starts here
const results = [];
const pending = new Set();
const order = elements.map((element,i)=>({element,i}));
while(order.length) {
const {element,i} = order.shift();
const promise = api(element);
pending.add(promise);
promise.then((result)=>{
pending.delete(promise);
results[i] = result;
});
if(pending.size >= MAX_INFLIGHT) {
await Promise.any(pending);
}
}
await Promise.all(pending);
return results;
// your code ends here
// ============
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment