Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Custom Retry Conditions #6

Open
skeggse opened this issue Feb 21, 2014 · 4 comments
Open

Custom Retry Conditions #6

skeggse opened this issue Feb 21, 2014 · 4 comments

Comments

@skeggse
Copy link

skeggse commented Feb 21, 2014

Retry is great for some use-cases, but it seems like it could be extended to support custom retry scenarios.

A good example might be OAuth: if I receive a 401 Unauthorized response, I want to update my access token. Naturally, that operation is asynchronous, so it'd need a callback.

var token;

getToken();

function getToken(callback) {
  oauth.getOAuthAccessToken(function(err, accessToken) {
    if (err)
      return callback && callback(err);
    token = accessToken
    callback && callback(null);
  });
}

superagent
  .get('https://segment.io/dosomethingsecret')
  // superagent-oauth
  .sign(oauth, token)
  // retry once
  .retry(1)
  // this API isn't entirely feasible, it's just an example
  // synchronously check whether to retry
  .when(function(err, res) {
    return res && res.unauthorized;
  })
  // only retry after this task finishes
  .after(1, function(next) {
    getToken(next);
  })
  .end(onresponse);

function onresponse(err, res) {
  // ...
}
@leviwheatcroft
Copy link

I'm 2.5 years late to this party but... it's looks like you can add custom conditions something like this:

const superagent = require('superagent')
const superagentRetry = require('superagent-retry')
superagentRetry(superagent)
superagentRetry.retries.unauthorized = (err, res) => res && res.unauthorized

retries property is exported here

@skeggse
Copy link
Author

skeggse commented Sep 12, 2016

Thanks for the note. That would certainly take care of the .when part of the example, but it doesn't solve the rest of the problem. Note that some details of my example are not feasible (the bound token wouldn't get reapplied to the request), so this specific use-case might be outside the scope of superagent-retry. That said, forcing subsequent retries to wait with setTimeout or wait until an async operation could be in-scope, and we might consider adding that functionality.

Also worth noting: the retries field is not documented, and as such I consider the custom retry condition undocumented behavior. Maybe if it had @api public in the jsdocs I'd use it, though ideally it'd be documented in the readme.

@FraBle
Copy link

FraBle commented Jun 8, 2018

@skeggse Did you find a solution for the OAuth token refresh scenario?

@yocontra
Copy link

It would be great if this library was able to be configured in a way to respect 429 (Rate Limit) and Retry-After headers, just to give an example case.

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

No branches or pull requests

4 participants