Retrying a request using 'request' library without extra modules I am using the request module and wanted to implement some retries for robustness. It seems excessive to use a whole new or extra module just to do a retries so I've put together the following as an exercise to retry requests using only the request library:

var checkUrl = function(url = 'http://localhost', retriesLeft = 0, timeoutMS = 3000) {
  var request = require('request');
  request(url, async function (error, response, body) {
    if (error) {
      if (retriesLeft &gt; 0) {
        console.log(url + ' : not available yet');
        await sleep(timeoutMS);
        checkUrl(url, --retriesLeft);
      }
    } else {
      console.log(url + ' : URL is available');
    }
  });

}

function sleep(ms) {
  return new Promise(resolve =&gt; setTimeout(resolve, ms));
}

checkUrl('http://www.example.com/', 3, 5000);


Is this a reasonable approach? It seems to work. It's just a basic example - I've not considered redirects for instance.

Of course this can be wrapped up to make it easier to use and then, ironically, becomes a sort of module but it would not require pulling anything external.
It's not "excessive" (your term) to use a pre-made module that does what you want. In fact, if it's solid code, then it's smart to use something that's already written and tested and maintained by someone else. There's little cost on a server to using a small module that does what you want.

I didn't do an exhaustive search, but I came across requestretry on NPM that seems to be pretty much what you might want (there are several other similar options on NPM too).  Using it, reduces your code to this:

const requestRetry = require('requestretry');

function checkUrl(url = 'http://localhost', retriesLeft = 0, timeoutMS = 3000) {
    return requestRetry({url: url, maxAttempts: retriesLeft + 1, retryDelay: timeoutMS});
}

checkUrl('http://www.example.com/', 3, 5000).then(data =&gt; {
    console.log(data);
}).catch(err =&gt; {
    console.log(err);
});


This particular library even has ways for you to plug in your own retry strategy or delay strategy (if, for example, you wanted to implement a back-off strategy to the retries so the retry interval gets longer after more failures which is sometimes desirable).