Pauses the execution for a specified number of milliseconds.
This is useful when you want to introduce delays in asynchronous code, without blocking the event loop.
Example:
console.log("Start"); awaitsleep(1000); // Pauses for 1 second console.log("End"); // Logs after 1 second
Parameters
ms: number
The number of milliseconds to pause execution.
Returns Promise<void>
A Promise that resolves after the specified time has passed.
Example
sleep(2000).then(() =>console.log("2 seconds have passed")); // Logs after 2 seconds
Pauses the execution for a specified number of milliseconds. This is useful when you want to introduce delays in asynchronous code, without blocking the event loop.
Example: