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

fluent-wait

v1.0.1

Published

This library features a fluent wait functionality which you can use to execute an action periodically until a certain condition is fulfilled. Useful for handling synchronization issues that may arise, e.g. in test automation.

Downloads

10

Readme

FluentWait Utility

This utility provides a fluent interface for creating a wait condition that periodically checks if a certain condition is met. It is useful when dealing with asynchronous operations where you need to wait for a certain condition to be fulfilled before proceeding.

Use

  • in async processes: wait until a batch job finishes until proceeding processing
  • in e2e UI tests: wait until all expected locators are found
  • ... many more cases.

Usage

First, import the FluentWait class and the PollingConfiguration type from the module:

import { FluentWait, PollingConfiguration } from './index';

Then, create a new instance of FluentWait:

let fluentWait = new FluentWait();

You can then chain the following methods to configure the wait condition:

  • withFunctionToExecute(functionToExecute): This method sets the function that will be executed periodically. The function can be synchronous or asynchronous, and it can optionally take an argument.

  • toBeFulfilledCondition(condition): This method sets the condition that will be checked after each execution of the function. The condition should be a function that takes the result of the function to execute as an argument and returns a boolean.

  • usePollingConfiguration(pollingConfiguration): This method sets the polling configuration. The polling configuration should be an object of type PollingConfiguration that specifies the interval and timeout of the polling.

  • useTimeoutCallBack(timeoutCallback): This method sets a callback function that will be called if the wait condition times out.

Finally, call the execute method to start the wait condition:

fluentWait.execute();

This will return a promise that resolves with the result of the function to execute when the condition is fulfilled, or rejects with an error if the wait condition times out.

Example

let fluentWait = await new FluentWait()
    .withFunctionToExecute(async () => {
        // This could be any asynchronous operation
        let result = await fetchSomeData();
        return result;
    })
    .toBeFulfilledCondition(result => result !== null)
    .usePollingConfiguration({ delayTime: 1000, timeout: 5000 })
    .useTimeoutCallBack(() => console.log('Timeout!'))
    .execute();

In this example, the fetchSomeData function is called every second until it returns a non-null result or until 5 seconds have passed. If the function still returns a null result after 5 seconds, the 'Timeout!' message is logged to the console.

Another example will use the standalone method and the .then() handling for promises:

import { fluentWait, PollingConfiguration } from './index';

async function fetchSomeData(): Promise<number | null> {
    // This could be any asynchronous operation
    // For the sake of this example, let's return a random number or null
    return Math.random() > 0.5 ? Math.random() : null;
}

const pollingConfiguration: PollingConfiguration = {
    delayTime: 1000,
    timeout: 5000,
    pollingStartAfter: 0
};

fluentWait(
    fetchSomeData,
    result => result !== null,
    pollingConfiguration,
    () => console.log('Timeout!')
).then(result => {
    if (result !== null) {
        console.log(`Fetched data: ${result}`);
    }
});