
In this code, Print first is displayed first and then the function execution is paused for 2 seconds before the next line of code is executed. The timer functions within Node.js implement a similar API as the timers API provided by Web Browsers but use a different internal implementation that is. In programming languages like C, Ruby, or Python pausing or delaying function execution is pretty straightforward as you would only need to call the sleep function.įor instance, consider the following Python function: import time However, there are different methods you can use to implement its functionality.

First, consider this code that uses setTimeout to delay printing a simple statement on the console after 2 seconds. So why not use it to implement the sleep () function. It returns a string which is either 'ok', 'not-equal', or 'timed-out'. The setTimeout () function is used to create a time delay before a certain block of code is executed. The Node.Js sleep function does not exist. The Atomics.wait () static method verifies that a given position in an Int32Array still contains a given value and if so sleeps, awaiting a wake-up notification or times out.


How can we sleep or delay using Node.Js Javascript?
