I'm trying to do a GET request in node.js. I would like to access the result of the request but there is a problem. Here is the code :
const https = require('https');
var data;
https.get('https://www.example.com', (resp) => {
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
console.log("Inside : "+data+"\n");
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
console.log("Outside : "+data+"\n");
The outside message appears before the inside one, and only the latter displays the request. That is due to asynchronous process I guess. But if I want to use my request outside, it's a problem because of this delay. How may I do?