p-do-whilst

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


Keywords
promise, do, whilst, while, loop, wait, condition, conditional, async, await, promises, bluebird
License
MIT
Install
npm install p-do-whilst@2.0.0

Documentation

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

const pDoWhilst = require('p-do-whilst');

(async () => {
	let count = 0;

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

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

API

pDoWhilst(action, condition)

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

action

Type: Function

Action to run for each iteration.

You can return a promise and it will be handled.

condition

Type: Function

Expected to return a boolean of whether to continue.

Related

  • p-whilst - While a condition returns true, calls a function repeatedly, and then resolves the promise
  • p-forever - Run promise-returning & async functions repeatedly until you end it
  • p-wait-for - Wait for a condition to be true
  • More…

License

MIT © Sindre Sorhus