Skip to content

Run promise-returning & async functions repeatedly until you end it

License

Notifications You must be signed in to change notification settings

sindresorhus/p-forever

Repository files navigation

p-forever Build Status

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.

const pForever = require('p-forever');

pForever(async i => {
	i++;

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

	await createFixture(i);

	return i;
}, 0);

or

const pForever = require('p-forever');

let i = 0;

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

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

	await createFixture(i);
});

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…

License

MIT © Sindre Sorhus

About

Run promise-returning & async functions repeatedly until you end it

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •