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

Add way to run code between each retry attempt

Open peterflynn opened this issue 5 years ago • 0 comments

For various use cases, you may want to run code after a failed attempt and before the next try. For example:

  • Want to log errors/warnings whenever a retry is needed or keep a count of how many retries something took on average
  • Want to do some cleanup after a failed attempt, e.g. clearing caches or closing a socket

You could argue that it could be better to do cleanup in a catch+rethrow at the end of the function being retried, but that's a bit more verbose (compared to encapsulating that boilerplate in the retry API itself). And for the first use case, where you want to log a warning on the second attempt (first retry) but not the first attempt, you have to resort to hacks like keeping a counter value somewhere.

Suggestion: add an option for a callback like beforeRetry which is run immediately before every retry attempt (but not before the very first attempt).

peterflynn avatar Aug 29 '19 23:08 peterflynn