A utility that polls a condition function until it returns true, with configurable intervals, timeouts, and abort signals.
p-wait-for is a Node.js library that repeatedly polls a condition function until it returns true, then resolves a promise. It solves the problem of waiting for asynchronous conditions—like file existence, API responses, or resource availability—with configurable intervals, timeouts, and abort signals. It's part of the 'promise-fun' collection of promise utilities.
Node.js developers who need to implement polling logic, wait for asynchronous conditions in tests or scripts, or handle timeouts and cancellations in promise-based workflows.
Developers choose p-wait-for for its simplicity, flexibility in configuring intervals and timeouts, and seamless integration with modern JavaScript features like AbortController. It's a lightweight, focused alternative to writing custom polling loops.
Wait for a condition to be true
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Allows setting custom check intervals via the 'interval' option, defaulting to 20ms for responsive waiting without over-polling, as shown in the README examples.
Supports custom timeout durations, error messages, and fallback functions, enabling graceful error recovery or alternative actions, detailed in the timeout options section.
Integrates with AbortController to cancel waits externally via the 'signal' option, useful for user-initiated cancellations or cleanup in async operations.
Provides pWaitFor.resolveWith() to resolve the promise with a specific value, enhancing flexibility in condition-based workflows, as demonstrated in the API documentation.
Lacks built-in support for exponential backoff, jitter, or other sophisticated retry mechanisms, which are common in more robust polling libraries for handling network or transient errors.
Fixed-interval polling can lead to unnecessary CPU usage or performance issues if the condition changes infrequently, with no automatic adjustment based on load or heuristics.
Timeout errors are generic unless manually customized; the library doesn't provide detailed context or logs on why the condition failed, making debugging more challenging.