Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sindresorhus/p-whilst
While a condition returns true, calls a function repeatedly, and then resolves the promise
https://github.com/sindresorhus/p-whilst
Last synced: 3 days ago
JSON representation
While a condition returns true, calls a function repeatedly, and then resolves the promise
- Host: GitHub
- URL: https://github.com/sindresorhus/p-whilst
- Owner: sindresorhus
- License: mit
- Created: 2015-11-17T17:48:47.000Z (about 9 years ago)
- Default Branch: main
- Last Pushed: 2024-10-16T14:39:19.000Z (3 months ago)
- Last Synced: 2025-01-18T07:07:10.539Z (10 days ago)
- Language: JavaScript
- Homepage:
- Size: 23.4 KB
- Stars: 54
- Watchers: 7
- Forks: 9
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
- promise-fun - p-whilst
README
# p-whilst
> While a condition returns true, calls a function repeatedly, and then resolves the promise
Think async version of the [`while` statement](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while).
## Install
```sh
npm install p-whilst
```## Usage
```js
import pWhilst from 'p-whilst';let count = 0;
await pWhilst(
() => count < 5,
() => count++
);console.log(count);
//=> 5
```## API
### pWhilst(condition, action, initialValue?)
While `condition` returns `true`, executes `action` repeatedly, and then resolves the promise to the result of the last call to `action`. Rejects if `action` returns a promise that rejects or if an error is thrown anywhere.
#### condition
Type: `Function`
Arguments: The value the `action` function returns or `initialValue` for the first iteration.Expected to return a `boolean` or a `Promise` that indicates whether to execute `action`.
#### action
Type: `Function`
Arguments: The value the last call to `action` function returns.Action to run for each iteration.
You can return a promise and it will be handled.
## Related
- [p-do-whilst](https://github.com/sindresorhus/p-do-whilst) - Calls a function repeatedly while a condition returns true 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)