I need to make two http-requests, which I would like to have in two seperated functions. I thought synchronously:
.. make the first http-request and return the response
.. pass the response to the second function which makes the second http-request
here the first function:
function getObjTypes(cfg) {
// request-options
var post_options = {
host: cfg.apiHost,
port: cfg.apiPort
};
// request
var ObjectTypes = [];
var post_req = http.request(post_options, function(res) {
res.setEncoding('utf8');
let resBody = '';
res.on('data', function (chunk) {
resBody += chunk;
});
res.on("end", () => {
var resObject = JSON.parse(resBody);
return ObjectTypes; // Here the ObjectTyes needs to be passed as a return
});
});
post_req.end();
}
I know whats is wrong.. the http-request is async, so the second function is called before the response fron the first function is there.. but how would I "fix" this?