

p-immediate - Returns a promise resolved in the next event loop - think setImmediate () p-timeout - Timeout a promise after a specified amount of time. Sometimes you want your function to pause. p-min-delay - Delay a promise a minimum amount of time.


However, this functionality is absent from JavaScript due to its asynchronous. Ryan Shillington at 22:06 9 let sleep require ('util').promisify (setTimeout) is three characters longer but reusable and more readable imo Brian H. Learn how to make your function sleep for a certain amount of time in JavaScript. Many programming languages have a sleep function that will delay a program’s execution for a given number of seconds. In vanilla JavaScript - we can use the built-in setTimeout() function to "sleep"/delay code execution: setTimeout( function ( ), 1000) Ĭonst timeout2 = setTimeout(printMessage, 2000) Ĭonsole. You have to be using NodeJS 7.6.0 or above though. In this short guide, we’ll learn how to wait in JavaScript - or rather, how to sleep/delay code execution, using the setTimeout() function. Note: Delaying Code Execution is colloquially known as "sleeping" or less commonly as "waiting".
