sleep-synchronously
Block the main thread for a given amount of time
This is similar to a native sleep()
function you can find in many other languages.
This is the wrong tool for most tasks! Prefer using async APIs whenever possible. This package can be useful in tests if you need to wait on some resource, for example, where you know it only takes 1 second but there's no good way to detect when it's ready.
This package is better than many other similar packages as it's not a native Node.js addon, but instead uses modern JavaScript features. It does not work in the browser.
Install
$ npm install sleep-synchronously
Usage
import sleepSynchronously from 'sleep-synchronously';
console.log(new Date());
sleepSynchronously(2000);
console.log(new Date());
API
sleepSynchronously(milliseconds)
Block the main thread for the given amount of milliseconds.
No other code will execute while it's sleeping, not even asynchronous code.
Related
- delay - Delay a promise a given amount of time
- make-synchronous - Make an asynchronous function synchronous