-1

I have copied the very good code from https://www.tomas-dvorak.cz/posts/nodejs-request-without-dependencies/ to make a http request in nodejs using native modules.

I want to be able to use the data value later on in the script.

I know this is a common issue with newbies and async code, i just CANNOT understand this yet and have struggled for weeks to get it.

I have coped much code, watched youtube, talked to people, its flippen hard..

const getContent = function(url) {
    return new Promise((resolve, reject) => {
      const https = require('https')
      const request = https.get(url, (response) => {
        // handle http errors
        if (response.statusCode < 200 || response.statusCode > 299) {
           reject(new Error('Failed to load page, status code: ' + response.statusCode));
         }
        // temporary data holder
        const body = [];
        // on every content chunk, push it to the data array
        response.on('data', (chunk) => body.push(chunk));
        // we are done, resolve promise with those joined chunks
        response.on('end', () => resolve(body.join('')));
      });
      // handle connection errors of the request
      request.on('error', (err) => reject(err))
      })
  }

getContent('https://myapi/json')
  .then((data) => console.log(data))
  .catch((err) => console.error(err))

// I want to use the "data" value down here in my script.  I want to do things with the "data" value like JSON.parse(data)

console.log(data) //undefined
let json = JSON.parse(data) //undefined
console.log('after')

my result for data is undefined

How can i use data down here below all the code above?

ajhstn
  • 101
  • 1
  • 3
  • 11
  • 2
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – tkausl Jan 22 '19 at 12:12
  • 1
    classic problem for async js newbies – shanks Jan 22 '19 at 12:17
  • @tkausl I don’t see an answer for my question there.. – ajhstn Jan 22 '19 at 12:27
  • @shanks are you able to help? – ajhstn Jan 22 '19 at 12:27
  • You'll see this issue cropping up for new users of Node.js quite a lot. Remember Node.js uses asynchronous I/O. The functions you register as callbacks will _not have run_ when you do your console.log. It's just a matter of playing about with the technology until you get comfortable with it. Good luck!! – Terry Lennox Jan 22 '19 at 12:28
  • 1
    "*later in the script*" will be inside the `then` callback, in place of that `console.log` call. – Bergi Jan 22 '19 at 12:34
  • 1
    Thank you all for your comments. I have learned that while @TerryLennox has answered the OP, I still do not completely comprehend node asynchronous and the node event loop. These concepts I need to go back to basics and keep playing with. I know I could use libraries like request-promise and others to make this easier, but I really want to only use native node and I only want to use libraries AFTER I understand native modules. Thanks all. – ajhstn Jan 22 '19 at 19:24
  • Also have a look at the async / await pattern. This is a more intuitive way of dealing with promises, while still using async io! – Terry Lennox Jan 22 '19 at 21:19

1 Answers1

1

You can setup a callback and access your data within this callback, this pattern should be easy enough to use.

getContent('https://myapi/json')
  .then(useData)
  .catch((err) => console.error(err))

// Use this callback to do what you want with your data!
function useData(data) { 
    console.log(data);
    let json = JSON.parse(data);
}

Or using async / await ( this might be more intuitive!):

async function testAwait() {
    let data = await getContent('https://myapi/json');
    console.log("data: ", data);
}

testAwait();
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40
  • That looks incredibly simplistic, wonder why I haven’t come across that before.. so are you saying I can just change the `let json =` to `var json =` and then use `json` later down in the script? – ajhstn Jan 22 '19 at 18:24
  • Well just bear in mind the sequence of events. You can assign the json variable in the callback, but it won't be populated until the callback is called. The best approach in Node.js is to use callbacks / handlers to process data. The flow of execution will not be as linear as in other technologies. – Terry Lennox Jan 22 '19 at 18:40