Skip to content
While a condition returns true, calls a function repeatedly, and then resolves the promise
JavaScript TypeScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.editorconfig
.gitattributes
.gitignore Cleanup Jun 30, 2017
.npmrc Cleanup Jun 30, 2017
.travis.yml Require Node.js 8, add TypeScript definition (#4) Mar 16, 2019
index.d.ts Refactor TypeScript definition to CommonJS compatible export (#5) Apr 3, 2019
index.js Refactor TypeScript definition to CommonJS compatible export (#5) Apr 3, 2019
index.test-d.ts
license
package.json 2.1.0 Apr 3, 2019
readme.md
test.js Require Node.js 8, add TypeScript definition (#4) Mar 16, 2019

readme.md

p-whilst Build Status

While a condition returns true, calls a function repeatedly, and then resolves the promise

Think async version of the while statement.

Install

$ npm install p-whilst

Usage

const pWhilst = require('p-whilst');

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

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

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

API

pWhilst(condition, action)

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.

condition

Type: Function
Arguments: The value the action function returns

Expected to return a boolean of whether to execute action.

action

Type: Function

Action to run for each iteration.

You can return a promise and it will be handled.

Related

  • p-do-whilst - Calls a function repeatedly while a condition returns true 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

You can’t perform that action at this time.