Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/awaitbox/sleep
Await for a certain amount of time.
https://github.com/awaitbox/sleep
async async-await async-functions await promise promise-library sleep sleep-timer
Last synced: about 1 month ago
JSON representation
Await for a certain amount of time.
- Host: GitHub
- URL: https://github.com/awaitbox/sleep
- Owner: awaitbox
- Created: 2018-01-05T23:06:26.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2018-01-07T05:21:48.000Z (about 7 years ago)
- Last Synced: 2024-12-06T16:17:34.741Z (about 2 months ago)
- Topics: async, async-await, async-functions, await, promise, promise-library, sleep, sleep-timer
- Language: JavaScript
- Size: 2.93 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
@awaitbox/sleep
========================Await for a certain amount of time.
#### `npm i @awaitbox/sleep --save`
The `sleep` async function returns a promise that will resolve after the
specified amount of time. This is a convenient alternative for `setTimeout`.
This is inspired from `sleep(...)` in the C language, except that time is
specified in milliseconds rather than seconds.You can use it in async functions:
```js
import sleep from '@awaitbox/sleep'async function main() {
await sleep( 1000 )
console.log( 'I fell asleep for a second!' )
}main()
```You can of course use it as a Promise:
```js
import sleep from '@awaitbox/sleep'sleep( 1000 )
.then( data => console.log( 'I took a 1-second nap!' ) )
```Chain values will pass through if you use it in a Promise chain like follows:
```js
import sleep from '@awaitbox/sleep'fetch( ... )
.then( ... )
.then( sleep.bind( null, 1000 ) ) // passes data through after 1 second
.then( data => console.log( 'use data for the awesome!', data ) )
```Note
----This is written in ES2016+ JavaScript. To use this in pre-ES2016 environments,
you'll need to run this through a transpiler like [Babel](http://babeljs.io)
(and I recommend using the
[fast-async](https://github.com/MatAtBread/fast-async) plugin to get the best
results). See some tips here on wiring it up with
[Webpack](https://webpack.js.org): http://2ality.com/2017/06/pkg-esnext.html.