# p-wait-for

> Wait for a condition to be true

Latest version **6.0.0** (published 2025-09-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install p-wait-for
pnpm add p-wait-for
yarn add p-wait-for
bun add p-wait-for
```

## Health

**Score 60/100 (C)** — status: stable.

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

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 6.0.0 |
| Published | 2025-09-21 |
| First published | 2016-10-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=20 |
| Dependencies | 0 |
| Unpacked size | 11.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 169 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | promise, wait, for, waits, condition, poll, polling, boolean, async, await, promises, bluebird |

## Links

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

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 6.0.0 (latest) — 2025-09-21
- 5.0.2 — 2023-03-30
- 5.0.1 — 2023-03-16
- 5.0.0 — 2022-07-29
- 4.1.0 — 2021-07-09
- 4.0.0 — 2021-04-06
- 3.2.0 — 2021-01-01
- 3.1.0 — 2019-04-02
- 3.0.0 — 2019-03-19
- 2.0.1 — 2018-11-07
- 2.0.0 — 2018-07-29
- 1.0.0 — 2016-10-21

## README

# p-wait-for

> Wait for a condition to be true

Can be useful for polling.

## Install

```sh
npm install p-wait-for
```

## Usage

```js
import pWaitFor from 'p-wait-for';
import {pathExists} from 'path-exists';

await pWaitFor(() => pathExists('unicorn.png'));
console.log('Yay! The file now exists.');
```

## API

### pWaitFor(condition, options?)

Returns a `Promise` that resolves when `condition` returns `true`. Rejects if `condition` throws or returns a `Promise` that rejects.

#### condition

Type: `Function`

Expected to return `Promise<boolean> | boolean` or a value from `pWaitFor.resolveWith()`.

#### options

Type: `object`

##### interval

Type: `number`\
Default: `20`

Number of milliseconds to wait after `condition` resolves to `false` before calling it again.

##### timeout

Type: `number | TimeoutOptions`\
Default: `Infinity`

Number of milliseconds to wait before automatically rejecting with a `TimeoutError`.

You can customize the timeout `Error` by specifying `TimeoutOptions`.

```js
import pWaitFor from 'p-wait-for';
import {pathExists} from 'path-exists';

await pWaitFor(() => pathExists('unicorn.png'), {
	timeout: {
		milliseconds: 100,
		message: new Error('Time’s up!')
	}
});

console.log('Yay! The file now exists.');
```

###### milliseconds

Type: `number`

Milliseconds before timing out.

Passing `Infinity` will cause it to never time out.

###### message

Type: `string | Error`

Specify a custom error message or error. If not specified, the default error message will be 'Promise timed out after {milliseconds} milliseconds' where {milliseconds} is replaced with the actual timeout value.

If you do a custom error, it's recommended to sub-class `TimeoutError`.

###### fallback

Type: `Function`

Do something other than rejecting with an error on timeout.

You could for example retry with more attempts.

Example:

```js
import pWaitFor from 'p-wait-for';
import {pathExists} from 'path-exists';

const result = await pWaitFor(() => pathExists('unicorn.png'), {
	timeout: {
		milliseconds: 50,
		fallback: () => {
			console.log('Time’s up! Executed the fallback function.');
			return 'default-value';
		},
	}
});

console.log(result); // 'default-value'
```

##### before

Type: `boolean`\
Default: `true`

Whether to run the check immediately rather than starting by waiting `interval` milliseconds.

Useful for when the check, if run immediately, would likely return `false`. In this scenario, set `before` to `false`.

##### signal

Type: `AbortSignal`

An `AbortSignal` to cancel the wait operation.

### pWaitFor.resolveWith(value)

Resolve the main promise with a custom value.

```js
import pWaitFor from 'p-wait-for';
import {pathExists} from 'path-exists';

const path = await pWaitFor(async () => {
	const path = getPath();
	return await pathExists(path) && pWaitFor.resolveWith(path);
});

console.log(path);
```

### TimeoutError

Exposed for instance checking.

## Related

- [p-whilst](https://github.com/sindresorhus/p-whilst) - Calls a function repeatedly while a condition returns true and then resolves the promise
- [More…](https://github.com/sindresorhus/promise-fun)

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