npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

p-wait-for

v5.0.2

Published

Wait for a condition to be true

Downloads

4,348,256

Readme

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.

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.

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

const originalSetTimeout = setTimeout;
const originalClearTimeout = clearTimeout;

sinon.useFakeTimers();

await pWaitFor(() => pathExists('unicorn.png'), {
	timeout: {
		milliseconds: 100,
		message: new MyError('Time’s up!'),
		customTimers: {
			setTimeout: originalSetTimeout,
			clearTimeout: originalClearTimeout
		}
	}
});

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

Type: number
Default: Infinity

Milliseconds before timing out.

Passing Infinity will cause it to never time out.

message

Type: string | Error Default: 'Promise timed out after 50 milliseconds'

Specify a custom error message or error.

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

customTimers

Type: object with function properties setTimeout and clearTimeout

Custom implementations for the setTimeout and clearTimeout functions.

Useful for testing purposes, in particular to work around sinon.useFakeTimers().

fallback

Type: Function

Do something other than rejecting with an error on timeout.

Example:

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

await pWaitFor(() => pathExists('unicorn.png'), {
	timeout: {
		milliseconds: 50,
		fallback: () => {
			console.log('Time’s up! executed the fallback function!');
		},
	}
});
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.

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…