I want to be able to set a variable with a simple var myVar = myFunction(); But myFunction has a callback inside of it and thus cannot return the value. I'm a bit lost on how I would convert this code to be able to return a value from the callback so that I could keep the simple variable definition.
#!/usr/bin/node
"use strict";
var http = require('http');
var myHappyVar = getTheData();
console.log(myHappyVar);
///// Functions /////
function getTheData() {
var data = '';
var options = {
host: 'google.com',
port: 80,
method: 'GET'
}
http.request(options, function(res) {
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function(){
console.log("DATA: " + data);
return data;
});
}).end();
}