Last active
December 20, 2019 12:38
-
-
Save erkanzileli/2ffe519e5f1038687cc01f36da9b87cf to your computer and use it in GitHub Desktop.
Run async tasks sequentally and with delay while without blocking event-loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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