Bluebird: Feature request: Promise.retry

Created on 28 May 2017  路  4Comments  路  Source: petkaantonov/bluebird

It would be really great if bluebird would have retry functionality built in. I have built this a few times and I have heard the same from colleagues.

Proposing a new top-level API Promise.retry. Would you be willing to have this in bluebird? If so, I am willing to contribute this.

Promise.retry(() => {
  // Will internally call Promise.resolve(resultOfThisFunction)
  // and potentially Promise.timeout() when a duration is specified
}, {
  // either retries or duration is required, a combination is possible
  retries: <number>,
  duration: <number, millis>,
  // optional, to support exponential backoff and randomized intervals
  getTryDelay(attempt: number): number {
    // exponential backoff
    return Math.pow(attempt, 2);
  }
})

Most helpful comment

Promises are composable, unless you can prove a performance benefit - this can be in a library:

let retry = async (fn, retries = 3, backoff = () => 100) => async (...args) => {
  for(let i = 0; i < retries; i++) {
    try { 
      await fn(...args);
    } catch (e) {
      if(i === retries - 1) throw e; 
      await backoff();    
    }
  }
};

All 4 comments

Is something like exponential backoff and randomized interval necessary?

@kontrafiktion: This could be an extension point. Maybe via an option which would allow the user to configure a delay until a next retry. I updated the API proposal to include this via getTryDelay

Promises are composable, unless you can prove a performance benefit - this can be in a library:

let retry = async (fn, retries = 3, backoff = () => 100) => async (...args) => {
  for(let i = 0; i < retries; i++) {
    try { 
      await fn(...args);
    } catch (e) {
      if(i === retries - 1) throw e; 
      await backoff();    
    }
  }
};

You might find p-retry useful.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jefflewis picture jefflewis  路  6Comments

SimonSchick picture SimonSchick  路  6Comments

overlookmotel picture overlookmotel  路  5Comments

rainabba picture rainabba  路  5Comments

ctrlplusb picture ctrlplusb  路  3Comments