# delay

> Delay a promise a specified amount of time

Latest version **7.0.0** (published 2025-10-31) · MIT license · 0 weekly downloads

## Install

```sh
npm install delay
pnpm add delay
yarn add delay
bun add delay
```

## Health

**Score 65/100 (B)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 7.0.0 |
| Published | 2025-10-31 |
| First published | 2012-07-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=20 |
| Dependencies | 2 |
| Unpacked size | 10.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 623 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | promise, resolve, delay, defer, wait, stall, timeout, settimeout, event, loop, next, tick, delay, async, await, promises, bluebird, threshold, range, random |

## Links

- npm: https://www.npmjs.com/package/delay
- Repository: https://github.com/sindresorhus/delay
- Homepage: https://github.com/sindresorhus/delay#readme
- Issues: https://github.com/sindresorhus/delay/issues
- Funding: https://github.com/sponsors/sindresorhus
- npm.io page: https://npm.io/package/delay

## Dependencies (2)

- [random-int](https://npm.io/package/random-int.md) ^3.1.0
- [unlimited-timeout](https://npm.io/package/unlimited-timeout.md) ^0.1.0

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 7.0.0 (latest) — 2025-10-31
- 6.0.0 — 2023-05-21
- 5.0.0 — 2021-02-01
- 4.4.1 — 2021-01-30
- 4.4.0 — 2020-07-18
- 4.3.0 — 2019-06-12
- 4.2.0 — 2019-04-08
- 4.1.0 — 2018-10-10
- 4.0.1 — 2018-09-10
- 4.0.0 — 2018-09-03
- 3.1.0 — 2018-08-20
- 3.0.0 — 2018-05-25
- 2.0.0 — 2017-03-22
- 1.3.1 — 2015-12-27
- 1.3.0 — 2015-12-25
- … 4 more at https://npm.io/package/delay/versions

## README

# delay

> Delay a promise a specified amount of time

> [!TIP]
> If you target Node.js only, you can use `import {setTimeout} from 'node:timers/promises'; await setTimeout(1000);` instead. This package can still be useful if you need browser support or the extra features.

## Install

```sh
npm install delay
```

## Usage

```js
import delay from 'delay';

bar();

await delay(100);

// Executed 100 milliseconds later
baz();
```

## API

### delay(milliseconds, options?) <sup>default import</sup>

Create a promise which resolves after the specified `milliseconds`.

### rangeDelay(minimum, maximum, options?)

Create a promise which resolves after a random amount of milliseconds between `minimum` and `maximum` has passed.

Useful for tests and web scraping since they can have unpredictable performance. For example, if you have a test that asserts a method should not take longer than a certain amount of time, and then run it on a CI, it could take longer. So with this method, you could give it a threshold instead.

#### milliseconds
#### mininum
#### maximum

Type: `number`

Milliseconds to delay the promise.

Unlike native `setTimeout`, this supports unlimited delay length.

#### options

Type: `object`

##### value

Type: `unknown`

A value to resolve in the returned promise.

```js
import delay from 'delay';

const result = await delay(100, {value: '🦄'});

// Executed after 100 milliseconds
console.log(result);
//=> '🦄'
```

##### signal

Type: [`AbortSignal`](https://developer.mozilla.org/docs/Web/API/AbortSignal)

The returned promise will be rejected with an `AbortError` if the signal is aborted.

```js
import delay from 'delay';

const abortController = new AbortController();

setTimeout(() => {
	abortController.abort();
}, 500);

try {
	await delay(1000, {signal: abortController.signal});
} catch (error) {
	// 500 milliseconds later
	console.log(error.name)
	//=> 'AbortError'
}
```

### clearDelay(delayPromise)

Clears the delay and settles the promise.

If you pass in a promise that is already cleared or a promise coming from somewhere else, it does nothing.

```js
import delay, {clearDelay} from 'delay';

const delayedPromise = delay(1000, {value: 'Done'});

setTimeout(() => {
	clearDelay(delayedPromise);
}, 500);

// 500 milliseconds later
console.log(await delayedPromise);
//=> 'Done'
```

### createDelay({clearTimeout, setTimeout})

Creates a new `delay` instance using the provided functions for clearing and setting timeouts. Useful if you're about to stub timers globally, but you still want to use `delay` to manage your tests.

```js
import {createDelay} from 'delay';

const customDelay = createDelay({clearTimeout, setTimeout});

const result = await customDelay(100, {value: '🦄'});

// Executed after 100 milliseconds
console.log(result);
//=> '🦄'
```

## Related

- [delay-cli](https://github.com/sindresorhus/delay-cli) - CLI for this module
- [unlimited-timeout](https://github.com/sindresorhus/unlimited-timeout) - `setTimeout`/`setInterval` that works with delays longer than 24.8 days
- [p-cancelable](https://github.com/sindresorhus/p-cancelable) - Create a promise that can be canceled
- [p-min-delay](https://github.com/sindresorhus/p-min-delay) - Delay a promise a minimum amount of time
- [p-immediate](https://github.com/sindresorhus/p-immediate) - Returns a promise resolved in the next event loop - think `setImmediate()`
- [p-timeout](https://github.com/sindresorhus/p-timeout) - Timeout a promise after a specified amount of time
- [More…](https://github.com/sindresorhus/promise-fun)

---
_Source: https://npm.io/package/delay · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
