3

I'm stuck with a small problem I think, but I can't find a way to solve it. I want to load a remote JSON in a variable, using Node.js HTTPS GET request. It's a success, but I can't use it anywhere else in my code. My function is the following (from Node.js doc):

function getMyFile() {
  var https = require('https');

  https.get('URL_I_am_targeting/file.json', (res) => {
    var { statusCode } = res;
    var contentType = res.headers['content-type'];

    let error;

    if (statusCode !== 200) {
      error = new Error('Request Failed.\n' +
                        `Status Code: ${statusCode}`);
    } else if (!/^application\/json/.test(contentType)) {
      error = new Error('Invalid content-type.\n' +
                        `Expected application/json but received ${contentType}`);
    }

    if (error) {
      console.error(error.message);
      // consume response data to free up memory
      res.resume();
    }

    res.setEncoding('utf8');
    let rawData = '';

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

    res.on('end', () => {
      try {
        const parsedData = JSON.parse(rawData);
        console.log(parsedData);
      } catch (e) {
        console.error(e.message);
      }
    });
  }).on('error', (e) => {
    console.error(`Got error: ${e.message}`);
  });
}

The request works fine, as I am able to log my .json in the console correctly. But my problem is that I can't use the constant "parsedData" anywhere else in my code. I tried to write my

return parsedData;

at several places in my function, but when I use the function in my code and try f. e.

var fileContent = getMyFile();
console.log(fileContent);

fileContent is undefined. It's like parsedData can't go out of the request. There is no error logged, the request goes fine, but I just can't use the content out of it.

I am not a Javascript professional, and it's probably something I get wrong with returning value from this request.

If somebody knows where I'm wrong and what could do the trick, it will be greatly appreciated ! Thanks !

31piy
  • 23,323
  • 6
  • 47
  • 67
Jason M
  • 33
  • 1
  • 1
  • 3
  • You can't return values from async functions. You either need to use `callback`, a `promise` or the newly defined `async / await` functions. I would suggest you start by learning about the callback nature of JS. – mattdevio Dec 27 '17 at 05:02
  • Indeed, I haven't noticed the async nature of this function, and didn't know how to create a Promise to handle it. I read [How do I return the response from an asynchronous call? – Robby Cornelissen](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) to understand, and answers from @31piy and js_noob were very helpful ! – Jason M Dec 27 '17 at 20:08

2 Answers2

20

You could write a promise-based wrapper for your function:

function getMyFile() {
  var https = require('https');

  return new Promise((resolve, reject) => {
      https.get('URL_I_am_targeting/file.json', (res) => {
        var { statusCode } = res;
        var contentType = res.headers['content-type'];

        let error;

        if (statusCode !== 200) {
          error = new Error('Request Failed.\n' +
            `Status Code: ${statusCode}`);
        } else if (!/^application\/json/.test(contentType)) {
          error = new Error('Invalid content-type.\n' +
            `Expected application/json but received ${contentType}`);
        }

        if (error) {
          console.error(error.message);
          // consume response data to free up memory
          res.resume();
        }

        res.setEncoding('utf8');
        let rawData = '';

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

        res.on('end', () => {
          try {
            const parsedData = JSON.parse(rawData);
            resolve(parsedData);
          } catch (e) {
            reject(e.message);
          }
        });
      }).on('error', (e) => {
        reject(`Got error: ${e.message}`);
      });

    });
  }

So that, you can use the function like this:

getMyFile()
  .then(response => {
    // handle success response here
  })
  .catch(error => {
    // handle error here
  });
31piy
  • 23,323
  • 6
  • 47
  • 67
0

function getMyFile() {
  return new Promise((resolve,reject)=>{
    //your logic and data manipulations here and finally resolve the variable
    
  resolve(parsedData)
  })
}

call it like this

getMyFile().then((parsedData)=>{
    //parsedData is the returned value u needed
}).catch((error)=>{
    //if any error handle it
})