1.1.1 • Published 2 years ago

repeat-promise-until-resolved v1.1.1

Weekly downloads
74
License
ISC
Repository
-
Last release
2 years ago

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

ParametertypeDescription
promiseFactoryfunctionA function that returns the promise you want to "repeat"
configobjectA configuration and hook object. Not required
config.maxAttemptsnumberNumber of maximum attempts(including the first one)
config.delaynumberNumber of milliseconds to wait between each retry. Useful in network requests
config.timeoutnumberNumber of milliseconds to wait, until the promise is manually rejected. Default is 8000
config.onAttemptfunctionHook into each attempt. Receives the number of the current attempt as an argument
config.onErrorfunctionHook into each failed attempt. Receives the Error object as an argument
config.shouldStopfunctionHook into each failed attempt. Receives the Error object as an argument. Return true, if you want the repetition to stop
1.1.1

2 years ago

1.1.0

2 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago