To avoid "callback hell". Promises with async/await allow you to write asynchronous code linearly, rather than nesting callbacks, which quickly becomes messy.
If you might need to clear the timeout before it executes, the callback version is probably better though (you could build a clearable promise version, but it would be less easy to work with). Depends on your use case.
Supposed in case, where you want to use sleep many times. If code goes long, say 1000 lines of code. In this case, it becomes hard to manage the code.
Here we can just use,
Why would you go through all that work of creating a javascript promise when you can just use the
setTimeout
function and theclearInterval
function?To avoid "callback hell". Promises with
async
/await
allow you to write asynchronous code linearly, rather than nesting callbacks, which quickly becomes messy.If you might need to clear the timeout before it executes, the callback version is probably better though (you could build a clearable promise version, but it would be less easy to work with). Depends on your use case.
agree
Supposed in case, where you want to use
sleep
many times. If code goes long, say 1000 lines of code. In this case, it becomes hard to manage the code.Here we can just use,
code and sleep 🥱