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 🙏

© 2024 – Pkg Stats / Ryan Hefner

await-until

v2.1.0

Published

Utils for waiting or running `predicate` fn after every `interval` until it returns `truthy` or run out of `timeout`. Timeout a promise or wait for a property mutation in an Object.

Downloads

11

Readme

awaitUntil

awaitUntil<T>(opts|predicate): Promise<T>

Runs predicate function after every interval until predicate function returns truthy or run out of timeout.

OPTIONS: an object of the following:

  • predicate: (args: { data: T | boolean | undefined, retriesSoFar: number, maxRetries: number, timeout: number }) => T | boolean | undefined - callback function (maybe async) to do whatever test needed at each interval, returns truthy to indicate success. Function is called with data from worker fn if worker fn is provided. Should be free to access any services or external data.
  • worker?: (args: { retriesSoFar: number, maxRetries: number, timeout: number }) => T | boolean | undefined - callback function (maybe async) to run and do something that returns a result to be analysed by predicate. If predicates accepts the result to conclude everything, the final result of worker is returned instead of the predicate. The reason there is a worker is that we may want to actually return falsey data from awaitUntil, that can be done by the worker, but the predicate can just return a falsey or truthy to indicate loop progress.
  • timeout? number - maximum number of ms to wait until predicates returns truth. defaults to 42000 or calculated from interval * maxRetries if provided.
  • interval?: number - interval between retries. defaults to 100 or calculated from timeout/maxRetries if provided.
  • maxRetries?: number - number of retries before we quit, use Infinity to indicate such. defaults to calculation of timeout/interval.
  • waitFirst?: boolean - wait 1 interval first before executing predicate function for the first time.
  • debug?: boolean - flag to show debug information.

Usage 1:

awaitUntil works without a worker and can be used to evaluate external or async data. eg: this will evaluate after every second. It will time out after 30seconds.

 let res = await awaitUntil({
    predicate: ()=>{/*evaluate/analyse result and return true/false to break/continue waiting*/},
    timeout: 30000, 
    interval: 1000
}).catch(result=>{/*do something when we have failed*/});

Usage 2:

Use a worker to produce data then predicate to evaluate the result. Helps code to look cleaner and avoid repeating the same process just to check if something is done or get its data.

eg: instead of doing this:

const selector = 'div';
/** wait until a certain element is available in the dom, default timeout will apply etc */
await awaitUntil({
    predicate: () => !!document.querySelectorAll(selector).length,
});
/** then re-get the same element now that it's available do something with it */
document.querySelectorAll(selector).forEach((el) => {
    // do something with element
});

eg: do this to query the selector only during the wait:

/** wait until a certain element is available in the dom, default timeout will apply etc */
const elements = await awaitUntil({
    worker: () => document.querySelectorAll('div'),
    predicate: ({ data }) => !!data.length,
});

elements.forEach((el) => {
    // do something with element
});

Usage 3:

or use it by passing the predicate as the only parameter, in this case it uses defaults as indicated above:

// this will try to get result within 42secs for 420 times 
let res = await awaitUntil(()=>{/*evaluate/analyse state return true/false to break/continue waiting*/});

NB: or all other promise methods can be used since this is a promise. You can catch the timeout when it happens as it throws on timeout.

promiseTimeout

promiseTimeout<T>(promise, timeout): Promise<T>

Useful for timing out given promise. It will throw if timeout ms is reached before promise settles with resolve or rejection.

waitFor()

waitFor(Record<any,any>, prop: string): Promise<any>

WIP* has a few issues on certain objects, but works

Fast, non-polling wait for mutation or existence of an object's property.

  • @param object {Object} - Object to check property on
  • @param property {String} - property to check for

useful when you don't want to do something before a property, function etc. is available.

Usage:

waitFor(app, 'someProperty').then(r=>{
 // something with the prop
 app.someProperty()
})

Return value is a promise that resolves to the value or the property.

sleep(ms)

sleep(ms: number): Promise<void>

Returns a promise that can be used to wait/sleep for given ms.

Author

Emmanuel Mahuni

MIT