DEV Community

Discussion on: Sleep function in javaScript

Collapse
 
lionelrowe profile image
lionel-rowe • Edited

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.

Collapse
 
ats1999 profile image
Rahul kumar

agree