Skip to content

Instantly share code, notes, and snippets.

@rafaelcamargo
Created April 25, 2025 19:08
Show Gist options
  • Save rafaelcamargo/1dadfc3814db96e60326741f87317923 to your computer and use it in GitHub Desktop.
Save rafaelcamargo/1dadfc3814db96e60326741f87317923 to your computer and use it in GitHub Desktop.
Async Task Runner
function processTasks(tasks){
if(!tasks?.length) return console.log('Finished');
const [exec, ...rest] = tasks;
setTimeout(() => {
exec();
processTasks(rest);
});
}
const smallBatch = [
() => console.log('Small A'),
() => console.log('Small B'),
() => console.log('Small C')
];
const regularBatch = new Array(200)
.fill({})
.map((item, index) => {
return function(){ console.log(`Regular ${index}`) };
});
const bigBatch = new Array(1000)
.fill({})
.map((item, index) => {
return function(){ console.log(`Big ${index}`) };
});
processTasks(bigBatch);
processTasks(smallBatch);
processTasks(regularBatch);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment