Home

Awesome

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.

Install

npm install p-do-whilst

Usage

Choose your preferred style:

import pDoWhilst from 'p-do-whilst';

let count = 0;

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

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

Or:

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