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

wait-until-condition

v1.3.0

Published

Executes a function until a condition is fulfilled with a truthy value

Downloads

63

Readme

Wait Until Condition

WaitUntil executes a function, expects a condition and waits until that condition is fulfilled with a truthy value. This can be used in applications and e2e tests as well.

Install

npm i wait-until-condition

As a Typescript decorator (a decorator can only be used in a class or method class)

import { waitUntil } from 'wait-until-condition/dist/decorator';
class Controller {
  constructor() {}
  
  @waitUntil({
    condition: (result: any) => result > .85,
    maxRetries: 6
  })
  public async process(shift: number): Promise<number> {
    return shift + Math.random();
  }
}

The condition function can be a class method as well, and the result will be exactly the same:

import { waitUntil } from 'wait-until-condition/dist/decorator';
class Controller {
  constructor() {}

  @waitUntil({
    condition: (result: any, context: any) => context.isProcessOk(result),
    maxRetries: 6
  })
  public async process(shift: number): Promise<number> {
    return shift + Math.random();
  }

  public isProcessOk(result: any) {
    return result > .85;
  }
}

You can simply call process as follow.
And the controller will execute until the condition is reached (result > .85), following the maximum amount of retries.

const controller: Controller = new Controller();
const response: number = await controller.process(0.1);
console.log(response);

As a composition

import { waitUntil } from 'wait-until-condition/dist/composition';
import { fibonacci } from 'wait-until-condition/dist/algorithms';
import { IConditionFunction } from 'wait-until-condition/dist/interfaces';

const process = async (shift: number): Promise<number> => {
  return shift + Math.random();
};

const condition: IConditionFunction = (result, context) => result > 0.9;
const response = await waitUntil<number>(process, {
  condition: condition,
  algorithm: fibonacci,
  maxRetries: 3,
  onRetry: (result, context) => console.log('onRetry'),
  onError: (error, context) => console.log(error),
  onRetriesComplete: (context) => console.log('onRetriesComplete'),
})(0.2);

console.log(response);

WaitUntil Options

condition: Function
Function which tests the response of the original function. If the condition returns true, then the response is returned. If false, the original function will be executed again until the number of retries has been reached.

maxRetries: number
Specifies the maximum number of retries.

algorithm: Function (optional)
Specifies how often the function must be called following a specific algorithm.
Can be one of the native algorithm function constant, fibonacci or a custom function.
Default is constant calling the method every second.
To use a native algorithm function, first import it from "wait-until-condition/dist/algorithms".
If you prefer a custom function, the custom function should implement IAlgorithmFunction.

onRetry: Function (optional)
Function called on each retry.
This function should implement IRetryFunction.

onRetriesComplete: Function (optional)
Function called when the condition has never been reached after all retries.
This function should implement IRetriesCompleteFunction.

onError: Function (optional)
Function called when an error occurs during the execution of the original function.
When an error occurs, it won't stop the process and the original function can be called again if the maximum number of retries have not been reached yet.
This function should implement IErrorFunction.