Search code examples
javascriptnode.jsrequesttimeouthttpwebrequest

How to repeat a "request" until success? NODEJS


I checked a few thread in StackOverflow, but nothing works for me. I have this request call and I need it to try to send the request until it succeed (but if it fails, it has to wait at least 3 seconds):

sortingKeywords.sortVerifiedPhrase = function(phrase) {

    var URL = "an API URL"+phrase; //<== Obviously that in my program it has an actual API URL
    request(URL, function(error, response, body) {
        if(!error && response.statusCode == 200) {
            var keyword = JSON.parse(body);
            if(sortingKeywords.isKeyMeetRequirements(keyword)){ //Check if the data is up to a certain criteria
                sortingKeywords.addKeyToKeywordsCollection(keyword); //Adding to the DB
            } else {
                console.log("doesn't meet rquirement");
            }
        } else {
            console.log(phrase);
            console.log("Error: "+ error);
        }

    });
};

Here's the weird part, if I call the same phrases in a row from the browser, it works almost without errors (it usually states: rate limit time esceeded).

Appreciate your help. Thanks in advance.


Solution

  • Here is a working program that I've written for this request. It sends the request via a function, if the request fails, it returns error handler and calls the function again.

    If the function succeeds the program returns a promise and quits executing.

    NOTE: if you enter an invalid url the programs exits right away, that's something that's have to do with request module, which I like to be honest. It lets you know that you have an invalid url. So you must include https:// or http:// in the url

    var request = require('request');
    
    var myReq;
    //our request function
    function sendTheReq(){
      myReq = request.get({
        url: 'http://www.google.com/',
        json: true
        }, (err, res, data) => {
        if (err) {
          console.log('Error:', err)
        } else if (res.statusCode !== 200) {
          console.log('Status:', res.statusCode)
        } else {
          // data is already parsed as JSON:
          //console.log(data);
        }
      })
    }
    
    sendTheReq();
    
    //promise function
    function resolveWhenDone(x) {
      return new Promise(resolve => {
        myReq.on('end', function(){
          resolve(x)
        })
        myReq.on('error', function(err){
          console.log('there was an error:  ---Trying again');
          sendTheReq();  //sending the request again
          f1();          //starting the promise again
        })
      });
    }
    
    //success handler
    async function f1() {
      var x = await resolveWhenDone(100);
      if(x == 100){
          console.log("request has been completed");
          //handle other stuff
      }
    }
    
    f1();