sleep-synchronously
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

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());
//=> Sun Aug 16 2020 14:28:54 GMT+0200 (Central European Summer Time)

sleepSynchronously(2000);

console.log(new Date());
//=> Sun Aug 16 2020 14:28:56 GMT+0200 (Central European Summer Time)

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

Package Sidebar

Install

npm i sleep-synchronously

Weekly Downloads

38

Version

2.0.0

License

MIT

Unpacked Size

3.98 kB

Total Files

5

Last publish

Collaborators

  • sindresorhus