tim-kos/node-retry

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

zkochan opened this issue · 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:

return true;

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

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

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

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