Comparing version 3.1.0 to 3.2.0
/** | ||
While `condition` returns `true`, executes `action` repeatedly, and then resolves the promise. Rejects if `action` returns a promise that rejects or if an error is thrown anywhere. | ||
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. | ||
@@ -24,3 +24,4 @@ @param condition - Expected to return a `boolean` or a `Promise<boolean>` that indicates whether to execute `action`. | ||
condition: (value: ValueType | undefined) => PromiseLike<boolean> | boolean, | ||
action: () => ValueType | PromiseLike<ValueType> | ||
): Promise<void>; | ||
action: (value: ValueType) => ValueType | PromiseLike<ValueType>, | ||
initialValue?: ValueType, | ||
): Promise<ValueType>; |
@@ -1,9 +0,11 @@ | ||
export default async function pWhilst(condition, action) { | ||
export default async function pWhilst(condition, action, initialValue) { | ||
const loop = async actionResult => { | ||
if (await condition(actionResult)) { | ||
return loop(await action()); | ||
return loop(await action(actionResult)); | ||
} | ||
return actionResult; | ||
}; | ||
return loop(); | ||
return loop(initialValue); | ||
} |
{ | ||
"name": "p-whilst", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "While a condition returns true, calls a function repeatedly, and then resolves the promise", | ||
@@ -15,2 +15,4 @@ "license": "MIT", | ||
"exports": "./index.js", | ||
"types": "./index.d.ts", | ||
"sideEffects": false, | ||
"engines": { | ||
@@ -17,0 +19,0 @@ "node": ">=12" |
@@ -9,5 +9,5 @@ # p-whilst | ||
```sh | ||
npm install p-whilst | ||
``` | ||
$ npm install p-whilst | ||
``` | ||
@@ -32,10 +32,10 @@ ## Usage | ||
### pWhilst(condition, action) | ||
### pWhilst(condition, action, initialValue?) | ||
While `condition` returns `true`, executes `action` repeatedly, and then resolves the promise. Rejects if `action` returns a promise that rejects or if an error is thrown anywhere. | ||
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 | ||
Type: `Function` | ||
Arguments: The value the `action` function returns or `initialValue` for the first iteration. | ||
@@ -47,2 +47,3 @@ Expected to return a `boolean` or a `Promise<boolean>` that indicates whether to execute `action`. | ||
Type: `Function` | ||
Arguments: The value the last call to `action` function returns. | ||
@@ -49,0 +50,0 @@ Action to run for each iteration. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4617
30
57