# p-forever

> Run promise-returning & async functions repeatedly until you end it

Latest version **4.0.0** (published 2026-02-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install p-forever
pnpm add p-forever
yarn add p-forever
bun add p-forever
```

## Health

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

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

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 4.0.0 |
| Published | 2026-02-02 |
| First published | 2016-11-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=20 |
| Dependencies | 0 |
| Unpacked size | 6.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 52 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | promise, forever, infinitely, infinite, while, whilst, repeat, repeatedly, recursive, recursion, times, number, count, loop, iterate, serial, serially, async, await, promises, bluebird |

## Links

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

## 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

- 4.0.0 (latest) — 2026-02-02
- 3.0.1 — 2021-05-22
- 3.0.0 — 2021-04-09
- 2.1.0 — 2019-04-03
- 2.0.0 — 2019-03-16
- 1.0.1 — 2017-06-07
- 1.0.0 — 2016-11-18

## README

# p-forever

> Run promise-returning & async functions until you end it

Think of it like an async version of `while (true) {}`.

## Install

```sh
npm install p-forever
```

## Usage

Here we create some numbered fixtures. The `createFixture()` function returns a Promise.

```js
import pForever from 'p-forever';

pForever(async index => {
	index++;

	if (index > 100) {
		return pForever.end;
	}

	await createFixture(index);

	return index;
}, {initialValue: 0});
```

or

```js
import pForever from 'p-forever';

let index = 0;

pForever(async () => {
	index++;

	if (index > 100) {
		return pForever.end;
	}

	await createFixture(index);
});
```

## API

### pForever(function_, options?)

Returns a `Promise` that is fulfilled when `function_` returns `pForever.end`, rejects if any of the promises returned from `function_` rejects, or rejects with an `AbortError` if the signal is aborted.

#### function_(previousValue)

Type: `Function`

Receives the previously returned value. If a `Promise` is returned, it's awaited before calling `function_` again.

#### options

Type: `object`

##### initialValue

Initial value to pass to `function_`.

##### signal

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

An `AbortSignal` to abort the loop from outside.

When aborted, the promise returned by `pForever` rejects with an `AbortError`.

```js
import pForever from 'p-forever';

const abortController = new AbortController();

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

await pForever(async () => {
	await someWork();
}, {signal: abortController.signal});
```

### pForever.end

Symbol used to end the loop.

## Related

- [p-times](https://github.com/sindresorhus/p-times) - Run promise-returning & async functions a specific number of times concurrently
- [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-forever · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
