3

I've got three components A, B and C. When A sends an HTTP request to B, B sends another HTTP request to C, retrieves the relative content and sends it back to A, as an HTTP response.

The B component is represented by the following Node.js snippet.

var requestify = require('requestify');

// sends an HTTP request to C and retrieves the response content
function remoterequest(url, data) {
  var returnedvalue;
  requestify.post(url, data).then(function(response) {
    var body = response.getBody();
    // TODO use body to send back to the client the number of expected outputs, by setting the returnedvalue variable
  });
  return returnedvalue;
}

// manages A's request
function manageanotherhttprequest() {
  // [...]
  var res = remoterequest(url, data);
  // possible elaboration of res
  return res;
}

I need to return body content as a result of the remoterequest function. I notices that, currently, the POST request is asynchronous. Hence, the returnedvalue variable is never assigned before returning it to the calling method.

How can I execute synchronous HTTP requests?

auino
  • 1,644
  • 5
  • 23
  • 43

1 Answers1

3

You are using restify which will return promise once its called for its methods(post, get..etc ). But the method you created remoterequest is not returning promise for you to wait using .then. You can make it to return promise using async-await or built-in promise as below:

  • using promise:

    var requestify = require('requestify');
    
    // sends an HTTP request to C and retrieves the response content
    function remoterequest(url, data) {
      var returnedvalue;
      return new Promise((resolve) => {
        requestify.post(url, data).then(function (response) {
          var body = response.getBody();
          // TODO use body to send back to the client the number of expected outputs, by setting the returnedvalue variable
        });
        // return at the end of processing
        resolve(returnedvalue);
      }).catch(err => {
        console.log(err);
      });
    }
    
    // manages A's request
    function manageanotherhttprequest() {
      remoterequest(url, data).then(res => {
        return res;
      });
    }
    
  • using async-await

    var requestify = require('requestify');
    
    // sends an HTTP request to C and retrieves the response content
    async function remoterequest(url, data) {
    
      try {
        var returnedvalue;
        var response = await requestify.post(url, data);
        var body = response.getBody();
        // TODO use body to send back to the client the number of expected outputs, by setting the returnedvalue variable
        // return at the end of processing
        return returnedvalue;
      } catch (err) {
        console.log(err);
      };
    }
    
    // manages A's request
    async function manageanotherhttprequest() {
        var res = await remoterequest(url, data);
        return res;
    }
    
kgangadhar
  • 4,886
  • 5
  • 36
  • 54
  • I don't understand why it should work. Being an asynchronous request, everything inside of the function is in practice executed after the calling method has returned. – auino May 28 '18 at 10:39
  • @auino, see i updated answer, please revert back if you have any question. – kgangadhar May 28 '18 at 13:16