Skip to content
Create a gist now

Instantly share code, notes, and snippets.

var Q = require("q");
var HTTP = require("q-http");
function httpReadRetry(url, timeout, times) {
return HTTP.read(url)
.then(function (content) {
return content;
}, function (error) {
if (times == 0)
throw new Error("Can't read " + JSON.stringify(url));
return Q.delay(timeout)
.then(function () {
return httpReadRetry(url, timeout, times - 1);
});
});
}
@mattgorecki

This was a huge help. Thank you.

@FrankHassanabad

Really useful. Appreciate it.

My version with es6 syntax added and a slightly different form:

var Q    = require('q');
var HTTP = require('q-http');

function httpReadRetry(url, timeout, times) {
  return HTTP.read(url)
  .then(content => content)
  .catch((error) => {
    if (times === 0)
      throw new Error(`Cant read ${JSON.stringify(url)}`);
    return Q.delay(timeout)
    .then(() => httpReadRetry(url, timeout, times - 1));
  });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.