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

repeat-promise-until-resolved

v1.1.1

Published

A utility for easily repeating a promise-based task, until it succeeds. Works both in Node and in the browser.

Downloads

541

Readme

A utility for easily repeating a promise-based task, until it succeeds. Works both in Node and in the browser.

Installation

$ npm install repeat-promise-until-resolved

Table of Contents

Examples

Usage

In a node-based environment:

const repeatPromiseUntilResolved = require('repeat-promise-until-resolved');   

If you're using it in the browser, without any build system, simply expose it to the global object:

 <script src="node_modules/repeat-promise-until-resolved/index.js"></script>
 

 

Basic example

const repeatPromiseUntilResolved = require('repeat-promise-until-resolved');
const axios = require('axios');

(async()=>{

    //Create a function that returns a promise, which you want to repeat until resolved. If you're not familiar with async functions, note that they return a promise.
    async function promiseFactory() {    
        const { data } = await axios('https://jsonplaceholder.typicode.com/todos/1')
        console.log(data)        
    }

    try {
        //Pass the promise-producing function, and an optional config object. The default maxAttempts value is 3.        
        await repeatPromiseUntilResolved(promiseFactory, { maxAttempts: 3});//Will be performed 3 times at most, until it finally fails.
    } catch (error) {//An error will be thrown, only after the final failing attempt.        
        console.log('final fail',error)
    } 
})()

  

 

Additional configuration


  (async()=>{
    async function promiseFactory() {
 
        const { data } = await axios('https://jsonplaceholder.typicode.com/todos/1')
        console.log('success',data)
    }

    //Hook into each attempt, and receive the attempt number
    const onAttempt = (attempt) => {
        console.log(attempt);
    }

    //Hook into every error thrown, and receive the Error object and the attempt number.
    const onError = (error, attempt) => {      
        console.log(error.message, attempt);
    }

    try {
        //Limit to 5 attempts, and create a delay of 2 seconds between each attempt. timeout: Wait up to 5 seconds for each promise to resolve, until it's manually rejected(and retried)
        //Also pass functions to the hooks.
        await repeatPromiseUntilResolved(promiseFactory, { maxAttempts: 5, delay: 2000, timeout:5000, onAttempt, onError });
    } catch (error) {        
        console.log('final fail',error)
    } 
})()

API

function repeatPromiseUntilResolved(promiseFactory,config):Promise

| Parameter | type | Description | | ----------- |---- | ------------------------------------------------------------ | | promiseFactory | function | A function that returns the promise you want to "repeat" | | config | object| A configuration and hook object. Not required| | config.maxAttempts | number| Number of maximum attempts(including the first one) | | config.delay | number| Number of milliseconds to wait between each retry. Useful in network requests | | config.timeout | number| Number of milliseconds to wait, until the promise is manually rejected. Default is 8000 | | config.onAttempt | function| Hook into each attempt. Receives the number of the current attempt as an argument | | config.onError | function| Hook into each failed attempt. Receives the Error object as an argument | | config.shouldStop | function| Hook into each failed attempt. Receives the Error object as an argument. Return true, if you want the repetition to stop |