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?