0

I am working on making my first NodeJS based Alexa skill and I ran into some asynchronous errors where the first run returns undefined and the second run outputs the call made in the first run. I looked into it and saw that properly fixing callbacks or implementing the Async module would fix it, but I'm not sure which is better (and why) and how exactly to use those solutions with my current code.

I'm looking for someone to explain the differences and how to implement the more proper solution into my skill.

Thanks!

"use strict"

const dbhVersion = 0.01;
var say;

//
// exported module
//

module.exports = {
  version: dbhVersion,
  //
  // Core Voice UI Helpers
  //
  getSomeStuff() {
    httpsGet(body => {
      const info = JSON.parse(body);
      say= info.x.y;
      console.log(say);
    });
   return say;
  }
};

function httpsGet(callback) {
    var options = {
       host: 'api.website.com',
       path: '/a/b/c',
       json: true,
       headers: {
         'Authorization': 'Bearer secretsecret'
       }
    };

    const https = require('https');

    var req = https.get(options, res => {
        res.setEncoding('utf8');
        var returnData = "";

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

        res.on('end', () => {
            callback(returnData);  
        });

    });
    req.end();
}
bmuir
  • 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) – James Aug 21 '18 at 14:31
  • @James I just saw that, going to look through that right now. Thanks! – bmuir Aug 21 '18 at 14:45

0 Answers0