const https = require('https');
async function A() {
let postData = JSON.stringify({
"f1": "D1",
"f2": "D2",
"encrypted": true
});
let options = {
hostname: "dummyurl1122.com",
path: "/api/wow",
method: "POST",
timestamp: new Date(),
headers: {
'Content-Type': 'application/json',
}
}
let chunks = [];
let reqGetToken = await https.request(options, (resGetToken) => {
if (resGetToken.statusCode != 200) {
console.log("***** Error = " + resGetToken.statusMessage);
return;
}
resGetToken.on("data", (d) => {
chunks.push(d);
});
resGetToken.on("end", async () => {
let output = JSON.parse(await (Buffer.concat(chunks)).toString());
return output.token; //****** I want this in output
});
});
reqGetToken.on("error", (e) => {
console.error(e);
});
reqGetToken.write(postData);
reqGetToken.end();
}
let output = await A(); // I want the output but it does not waits for the https response
console.log(output + 2);
When I call the function A it does not waits for the response and goes to next line. How can I make it wait for the output and then go to next line. The function A calls a external POST api and I need its response in output and then execute further lines.