Stack Overflow Asked by dashman on December 22, 2020
I’ve got a code segment that requires nested set-timeouts. Do something…wait for a while and then do something and wait for a while.
setTimeout( () =>
{
// do stuff..
//
setTimeout () =>
{
// some more
}, 2000 );
}, 1000 );
Is there a better way to implement above in modern Javascript – maybe using Promise.
function sleep(time) {
return new Promise(res => setTimeout(res, time))
}
async function doThings() {
await sleep(1000)
// do stuff
await sleep(2000)
// some more
}
Correct answer by Rubydesic on December 22, 2020
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP