Sleep holds off your function execution for specified time. This is super useful whenever you want to hold off something for ‘x’ time.
Here are two ways in which you can get your program to sleep for a while.
setTimeOut
|
|
Promise / async
-await
You could async the whole function to make it cleaner but there is no visible change to how your program executes :)
|
|
Note that -
-
The program itself does not go to sleep, only the function with timeout statement does. See details on Javascript event loop to know how Javascript processes your program instructions.
-
Do not use this sleep pattern for checking things and doing stuff. Use promises instead.