https://github.com/ai/nanodelay
A tiny (37 bytes) Promise wrapper around setTimeout
https://github.com/ai/nanodelay
Last synced: about 1 year ago
JSON representation
A tiny (37 bytes) Promise wrapper around setTimeout
- Host: GitHub
- URL: https://github.com/ai/nanodelay
- Owner: ai
- License: mit
- Created: 2017-10-21T20:07:33.000Z (over 8 years ago)
- Default Branch: main
- Last Pushed: 2023-07-20T17:46:54.000Z (almost 3 years ago)
- Last Synced: 2025-03-28T09:01:54.800Z (about 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 1.22 MB
- Stars: 196
- Watchers: 3
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Nano Delay
A tiny `Promise` wrapper around `setTimeout` for JavaScript.
Returns a `Promise` and resolve it after a specific amount of time.
* Only **45 bytes** (minified and gzipped),
10 times smaller than [`delay`] library.
* Has good **ES modules** and **TypeScript** support.
```js
import { delay } from 'nanodelay'
async function foo () {
await delay(300)
// Executed after 300 milliseconds
}
delay(300).then(() => {
// Executed after 300 milliseconds
})
```
[`delay`]: https://github.com/sindresorhus/delay
## Usage
The second argument will be used in `Promise` as resolved value
(useful to pass variables between scopes):
```js
createClient().then(client => {
expect(client).toBeOK
return delay(50, client)
}).then(client => {
expect(client).toBeConnected()
})
```
For quick hacks you can load Nano Delay from CDN. Do not use it in production because of low performance.
```js
import { delay } from 'https://cdn.jsdelivr.net/npm/nanodelay/index.js'
```
## Future
Node.js 16+ has built-in Promise-based `setTimeout`. After January 2023
you can remove `nanodelay` from your dependencies.
```js
import { setTimeout } from 'timers/promises'
await setTimeout(5000)
```