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

Custom Retry Conditions #6

Description

@skeggse

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) {
  // ...
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions