Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
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?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report

0

This is what you need:

(async function () {
  const https = require('https');
  var data;

  await new Promise((resolve, reject) => {https.get('https://www.example.com', (resp) => {

      resp.on('data', (chunk) => {
        data += chunk;
      });

      resp.on('end', () => {
      console.log("\n\n===>Inside : "+data+"\n");
      resolve();

      });

    }).on("error", (err) => {
      console.log("Error: " + err.message);
      reject();
    });
    
  });

  console.log("\n\n===>Outside : "+data+"\n"); 
})();
about 4 years ago · Juan Pablo Isaza Report

0

Thanks for answering. After searches, I made this, which work, but I don't know if it's the best way to do this.

const https = require('https');
var data;

(async function () {

  var p = new Promise((resolve) => {https.get('https://www.example.com', (resp) => {

      resp.on('data', (chunk) => {
        data += chunk;
      });

      resp.on('end', () => {
      console.log("\n\n===>Inside : "+data+"\n");
      resolve();

      });

    }).on("error", (err) => {
      console.log("Error: " + err.message);
    });
    
  });

  await p;
  console.log("\n\n===>Outside : "+data+"\n"); 
})();

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!