# p-do-whilst

> Calls a function repeatedly while a condition returns true and then resolves the promise

Latest version **2.1.0** (published 2024-08-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install p-do-whilst
pnpm add p-do-whilst
yarn add p-do-whilst
bun add p-do-whilst
```

## Health

**Score 30/100 (F)** — status: abandoned.

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

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2024-08-29 |
| First published | 2017-06-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=12 |
| Dependencies | 0 |
| Unpacked size | 4.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 38 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | promise, do, whilst, while, loop, wait, condition, conditional, async, await, promises, bluebird |

## Links

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

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

- 2.1.0 (latest) — 2024-08-29
- 2.0.0 — 2021-04-09
- 1.1.0 — 2019-04-03
- 1.0.0 — 2019-03-16
- 0.1.0 — 2017-06-30

## README

# p-do-whilst

> Calls a function repeatedly while a condition returns true and then resolves the promise

Think async version of the [`do…while` statement](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/do...while).

## Install

```sh
npm install p-do-whilst
```

## Usage

Choose your preferred style:

```js
import pDoWhilst from 'p-do-whilst';

let count = 0;

await pDoWhilst(
	() => count++,
	() => count < 5
);

console.log(count);
//=> 5
```

Or:

```js
import pDoWhilst from 'p-do-whilst';

const count = await pDoWhilst(
	currentCount => currentCount + 1,
	currentCount => currentCount < 5,
	0
);

console.log(count);
//=> 5
```

## API

### pDoWhilst(action, condition, initialValue?)

Executes `action` repeatedly while `condition` returns `true` and then resolves to the result of the last call to `action`. Rejects if `action` returns a promise that rejects or if an error is thrown anywhere.

#### action

Type: `Function`\
Arguments: The value the last call to `action` function returns or `initialValue` for the first iteration.

Action to run for each iteration.

You can return a promise and it will be handled.

#### condition

Type: `Function`\
Arguments: The value the `action` function returns.

Expected to return a `boolean` or a `Promise<boolean>` of whether to continue.

## Related

- [p-whilst](https://github.com/sindresorhus/p-whilst) - While a condition returns true, calls a function repeatedly, and then resolves the promise
- [p-forever](https://github.com/sindresorhus/p-forever) - Run promise-returning & async functions repeatedly until you end it
- [p-wait-for](https://github.com/sindresorhus/p-wait-for) - Wait for a condition to be true
- [More…](https://github.com/sindresorhus/promise-fun)

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