sleep-anywhere
Version:
A sleep function you can use anywhere
63 lines (43 loc) • 2.17 kB
Markdown
[](https://www.npmjs.org/package/sleep-anywhere)
[](https://www.npmjs.org/package/sleep-anywhere)
[](https://github.com/75lb/sleep-anywhere/network/dependents?dependent_type=REPOSITORY)
[](https://github.com/75lb/sleep-anywhere/network/dependents?dependent_type=PACKAGE)
[](https://github.com/75lb/sleep-anywhere/actions/workflows/node.js.yml)
[](https://github.com/feross/standard)
<a name="module_sleep-anywhere"></a>
## sleep-anywhere
An isomorphic sleep function you can use anywhere. Only required in the browser or isomorphic code - if your code runs on NodeJs only use this instead:
```js
import { setTimeout as sleep } from 'node:timers/promises'
```
**Example**
```js
import sleep from 'sleep-anywhere'
const result = await sleep(5000, 'later')
console.log('5s', result)
// 5s later
```
<a name="exp_module_sleep-anywhere--sleep"></a>
### sleep(ms, [returnValue]) ⇒ <code>Promise</code> ⏏
Returns a promise which fulfils after `ms` milliseconds with the supplied `returnValue`.
**Kind**: Exported function
| Param | Type | Description |
| --- | --- | --- |
| ms | <code>number</code> | How long in milliseconds to sleep for. |
| [returnValue] | <code>\*</code> | The value to return. |
### Load anywhere
This library is compatible with Node.js, the Web and any style of module loader. It can be loaded anywhere, natively without transpilation.
Node.js CommonJS:
```js
const sleep = require('sleep-anywhere')
```
Node.js ECMAScript Module:
```js
import sleep from 'sleep-anywhere'
```
Within a modern browser ECMAScript Module:
```js
import sleep from './node_modules/sleep-anywhere/index.js'
```
* * *
© 2018-24 Lloyd Brookes \<75pound@gmail.com\>.