node-retry icon indicating copy to clipboard operation
node-retry copied to clipboard

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

Open zkochan opened this issue 4 years ago • 4 comments

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:

https://github.com/tim-kos/node-retry/blob/c402d770d7362a8f3ee35c019a329f1f395fc4cc/lib/retry_operation.js#L92

zkochan avatar Jun 05 '20 23:06 zkochan

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`)
})

adrianObel avatar Dec 15 '20 22:12 adrianObel

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

zkochan avatar Dec 15 '20 22:12 zkochan

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

tim-kos avatar Dec 16 '20 07:12 tim-kos

👍 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.

vincerubinetti avatar May 04 '21 21:05 vincerubinetti