Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to decide if retry should be attempted? #51

Open
salmanm opened this issue Jan 23, 2019 · 3 comments
Open

Add option to decide if retry should be attempted? #51

salmanm opened this issue Jan 23, 2019 · 3 comments

Comments

@salmanm
Copy link

salmanm commented Jan 23, 2019

I know there's feature to bail out conditionally, but I think it would be useful to have a function that is called as soon as the operation fails, and the return value of that function should decide whether or not the next retry should happen.

So if (!op.retry(err)) { would just become something like if (!options.shouldRetry(err) || !op.retry(err)) {.

Current alternative to this would be to have a try/catch in the retrier function, decide in catch, throw if need to retry, and swallow and bail if needed. But this is a lot of boilerplate and can not be extracted into a separate function.

While having a function in option would be much easier.

Did it make sense?

@ghost
Copy link

ghost commented Jan 28, 2019

Hey

I am trying to do same as you, can you please provide the sample code to get it to work

thanks

@teebot
Copy link

teebot commented Dec 22, 2019

That's not so much boilerplate you can bail return in a catch if it matches your condition otherwise rethrow. Here's an example from an intercom api call:

await retry(async bail => {
      try {
        const res = await client.users.create(intercomUser)

        if ([400, 401, 409, 500].indexOf(res.statusCode) !== -1) {
          // don't retry
          bail(new Error(`Could not retry: status code ${res.statusCode}`))
        }
      } catch (error) {
        // fail silently don't bail
        if (/Multiple existing/.test(error.message)) {
          return
        }
        throw (error)
      }
    })

EDIT: do not bail in the catch, somehow it throws unhandeld

@ChuckJonas
Copy link

Not being able to "bail" in the catch is a huge limitation. Most of the time you would want to bail based on an thrown error condition...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants