Undersanding request get in node.js

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?


Your code execute asyncronymos. http.get(…) execute, but programm don’t wait result of your get. So when you do “console.log(“Outside : “+data+”n”);” – your data is empty. You will never get data to outside message, without js constructions like async/await.