p-wait-for vs async-wait-until vs promise-poller
Waiting for a condition versus retrying a task
p-wait-forasync-wait-untilpromise-poller

Waiting for a condition versus retrying a task

The decisive difference is what counts as success, what consumes a retry budget and what a timeout actually stops.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
p-wait-for3,065,29217013.5 kB0a month agoMIT
async-wait-until223,88981541 kB3a year agoMIT
promise-poller20,218118-87 years agoMIT

Decide what completion means

async-wait-until waits until its predicate produces a truthy value and resolves with that value. p-wait-for expects a boolean condition, with resolveWith available when a successful check should return another result. Rejected predicates are not automatically equivalent to a false condition; define error handling intentionally.

promise-poller follows a different task contract. A fulfilled promise can count as success even when it resolves to false, while a bare false return is a cancellation signal. Its shouldContinue callback determines whether polling continues after a result. These details make it unsuitable to swap into a boolean polling loop without adapting the task.

Budgets and timeouts

In promise-poller, a retries setting counts failed attempts including the initial attempt. Successful checks that continue through shouldContinue do not consume that same failure budget. Read the strategy and continuation behavior together rather than translating the option into a generic number of retries after the first call.

p-wait-for provides an overall timeout and an AbortSignal for stopping the wait. A pending condition is not thereby cancelled; its eventual result is ignored. Similarly, promise-poller's per-attempt timeout does not terminate the underlying asynchronous operation. If that operation has side effects, its own cancellation and lifecycle need separate coordination.

Practical selection

Choose the two wait-until tools for readiness checks with a clear condition. Choose promise-poller when retry strategy and task-result semantics are genuinely needed. Set an appropriate overall stopping condition, decide how errors affect the loop, and avoid overlapping work when a timed-out attempt can still finish later.

How to Choose: p-wait-for vs async-wait-until vs promise-poller

  • p-wait-for:

    Choose boolean-condition polling with an overall timeout, abort signal and explicit result-value support.

  • async-wait-until:

    Choose a condition that becomes truthy and return the satisfying value, with interval and overall timeout options.

  • promise-poller:

    Choose configurable task polling and retry strategies when its success, cancellation and continuation rules match the task.

README for p-wait-for

p-wait-for

Wait for a condition to be true

Can be useful for polling.

Install

npm install p-wait-for

Usage

import pWaitFor from 'p-wait-for';
import {pathExists} from 'path-exists';

await pWaitFor(() => pathExists('unicorn.png'));
console.log('Yay! The file now exists.');

API

pWaitFor(condition, options?)

Returns a Promise that resolves when condition returns true. Rejects if condition throws or returns a Promise that rejects.

condition

Type: Function

Expected to return Promise<boolean> | boolean or a value from pWaitFor.resolveWith().

options

Type: object

interval

Type: number
Default: 20

Number of milliseconds to wait after condition resolves to false before calling it again.

timeout

Type: number | TimeoutOptions
Default: Infinity

Number of milliseconds to wait before automatically rejecting with a TimeoutError.

You can customize the timeout Error by specifying TimeoutOptions.

A pending condition is not cancelled; its eventual result or error is ignored.

import pWaitFor from 'p-wait-for';
import {pathExists} from 'path-exists';

await pWaitFor(() => pathExists('unicorn.png'), {
	timeout: {
		milliseconds: 100,
		message: new Error('Time’s up!')
	}
});

console.log('Yay! The file now exists.');
milliseconds

Type: number

Milliseconds before timing out.

Passing Infinity will cause it to never time out.

message

Type: string | Error

Specify a custom error message or error. If not specified, the default error message will be 'Promise timed out after {milliseconds} milliseconds' where {milliseconds} is replaced with the actual timeout value.

If you do a custom error, it's recommended to sub-class TimeoutError.

fallback

Type: Function

Do something other than rejecting with an error on timeout.

You could for example retry with more attempts.

Example:

import pWaitFor from 'p-wait-for';
import {pathExists} from 'path-exists';

const result = await pWaitFor(() => pathExists('unicorn.png'), {
	timeout: {
		milliseconds: 50,
		fallback: () => {
			console.log('Time’s up! Executed the fallback function.');
			return 'default-value';
		},
	}
});

console.log(result); // 'default-value'
before

Type: boolean
Default: true

Whether to run the check immediately rather than starting by waiting interval milliseconds.

Useful for when the check, if run immediately, would likely return false. In this scenario, set before to false.

signal

Type: AbortSignal

An AbortSignal to cancel the wait operation.

A pending condition is not cancelled; its eventual result or error is ignored.

pWaitFor.resolveWith(value)

Resolve the main promise with a custom value.

import pWaitFor from 'p-wait-for';
import {pathExists} from 'path-exists';

const path = await pWaitFor(async () => {
	const path = getPath();
	return await pathExists(path) && pWaitFor.resolveWith(path);
});

console.log(path);

TimeoutError

Exposed for instance checking.

Related

  • p-whilst - Calls a function repeatedly while a condition returns true and then resolves the promise
  • More…