Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sindresorhus/p-do-whilst
Calls a function repeatedly while a condition returns true and then resolves the promise
https://github.com/sindresorhus/p-do-whilst
Last synced: 1 day ago
JSON representation
Calls a function repeatedly while a condition returns true and then resolves the promise
- Host: GitHub
- URL: https://github.com/sindresorhus/p-do-whilst
- Owner: sindresorhus
- License: mit
- Created: 2017-06-30T18:41:53.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2024-08-29T16:10:16.000Z (5 months ago)
- Last Synced: 2024-10-29T22:38:29.873Z (3 months ago)
- Language: JavaScript
- Size: 17.6 KB
- Stars: 36
- Watchers: 6
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
- promise-fun - p-do-whilst
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` 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)