Skip to content

It would be useful if the retry function returned the timout number #73

Description

@zkochan

I am working on improved logging in pnpm. When a request fails, I want to print something like:

WARN GET <URL> error (500). Will retry in 16 seconds. 2 retries left.

I can calculate how many retries are left but I can't know how long the retry will wait. So it would be great if the retry function would return timeout instead of true here:

return true;

Activity

  1. adrianObel commented on Dec 15, 2020

    @adrianObel

    For anyone interested, I found a way to do this. The retrier stores an array of intervals in its context that you can use to know the ms of the following attempt.

    To achieve something similar to what @zkochan is tried you could have something like

    const operation = retry.operation()
    
    operation.attempt(function (currentAttempt) {
      const nextTick = Math.ceil(this._timeouts[0] / 1000) 
      console.log(`Will retry in ${nextTick} seconds`)
    })
    
  2. zkochan commented on Dec 15, 2020

    @zkochan
    Author

    For now, I forked the repository and added the features that I needed: https://github.com/zoli-forks/node-retry

  3. tim-kos commented on Dec 16, 2020

    @tim-kos
    Owner

    Why not PR your desired changes? I'd approve them.

  4. vincerubinetti commented on May 4, 2021

    @vincerubinetti

    👍 for this. This would most conveniently be provided along with currentAttempt to the callback function in operation.attempt(fn). Yeah you can calculate it with the formula or with the retry.timeouts function, but it would be a good convenience to the user to just provide it. It feels like it belongs in that same spot, as users might want to log progress like Retry #4, trying again in 20 seconds.

    Presumably the library already has that value ready to go and can just pass it to the function.

  5. bybraveHQ commented on Jul 5, 2026

    @bybraveHQ

    Added in the maintained fork @bybrave/retry2: operation.getTimeout() returns the delay in milliseconds before the next scheduled attempt (or null before the first retry), without changing the boolean contract of retry():

    if (op.retry(err)) {
      console.warn(`request failed, retrying in ${op.getTimeout()}ms`);
      return;
    }

    Handy for exactly the pnpm-style "will retry in N seconds" logging. npm i @bybrave/retry2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions