The decisive difference is what counts as success, what consumes a retry budget and what a timeout actually stops.
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.
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.
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.
Choose boolean-condition polling with an overall timeout, abort signal and explicit result-value support.
Choose a condition that becomes truthy and return the satisfying value, with interval and overall timeout options.
Choose configurable task polling and retry strategies when its success, cancellation and continuation rules match the task.
Wait for a condition to be true
Can be useful for polling.
npm install p-wait-for
import pWaitFor from 'p-wait-for';
import {pathExists} from 'path-exists';
await pWaitFor(() => pathExists('unicorn.png'));
console.log('Yay! The file now exists.');
Returns a Promise that resolves when condition returns true. Rejects if condition throws or returns a Promise that rejects.
Type: Function
Expected to return Promise<boolean> | boolean or a value from pWaitFor.resolveWith().
Type: object
Type: number
Default: 20
Number of milliseconds to wait after condition resolves to false before calling it again.
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.');
Type: number
Milliseconds before timing out.
Passing Infinity will cause it to never time out.
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.
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'
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.
Type: AbortSignal
An AbortSignal to cancel the wait operation.
A pending condition is not cancelled; its eventual result or error is ignored.
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);
Exposed for instance checking.