Skip to content

Instantly share code, notes, and snippets.

@erkanzileli
Last active December 20, 2019 12:38
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 erkanzileli/2ffe519e5f1038687cc01f36da9b87cf to your computer and use it in GitHub Desktop.
Save erkanzileli/2ffe519e5f1038687cc01f36da9b87cf to your computer and use it in GitHub Desktop.
Run async tasks sequentally and with delay while without blocking event-loop
async function getNumber(number) {
return number
}
let tasks = [
() => getNumber(1),
() => getNumber(2),
() => getNumber(3),
() => getNumber(4)
];
(async function () {
tasks = tasks.map(task => () => new Promise(resolve => {
setTimeout(async () => {
resolve(await task())
}, 3000);
}))
let seconds = 0
setInterval(() => {
console.log('seconds', seconds++)
}, 1000);
for (let task of tasks) {
console.log(await task())
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment