p-forever
TypeScript icon, indicating that this package has built-in type declarations

3.0.1 • Public • Published

p-forever

Run promise-returning & async functions until you end it

Think of it like an async version of while (true) {}.

Install

$ npm install p-forever

Usage

Here we create some numbered fixtures. The createFixture() function returns a Promise.

import pForever from 'p-forever';

pForever(async index => {
	index++;

	if (index > 100) {
		return pForever.end;
	}

	await createFixture(index);

	return index;
}, 0);

or

import pForever from 'p-forever';

let index = 0;

pForever(async () => {
	index++;

	if (index > 100) {
		return pForever.end;
	}

	await createFixture(index);
});

API

pForever(fn, initialValue?)

Returns a Promise that is fulfilled when fn returns pForever.end, or rejects if any of the promises returned from fn rejects.

fn(previousValue)

Type: Function

Receives the previously returned value. If a Promise is returned, it's awaited before calling fn again.

initialValue

Initial value to pass to fn.

pForever.end

Symbol used to end the loop.

Related

  • p-times - Run promise-returning & async functions a specific number of times concurrently
  • p-whilst - Calls a function repeatedly while a condition returns true and then resolves the promise
  • More…

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 3.0.1
    953
    • latest

Version History

Package Sidebar

Install

npm i p-forever

Weekly Downloads

2,514

Version

3.0.1

License

MIT

Unpacked Size

5 kB

Total Files

5

Last publish

Collaborators

  • sindresorhus