This question already has an answer here:
I'm trying to return a value through different functions but it dosn't work for me. I think I have some trouble with callbacks and return in functions and I hope you guys can tell my mistake is.
My code are:
server.js
var tmdb = require('./tmdb')();
var result = tmdb.searchMovie("Alien", 1979, "de", false);
console.log(result);
tmdb.js
module.exports = function(){
var Tmdb = function() {};
Tmdb.prototype.searchMovie = function(title, year, lang, includeAdult) {
...
movieResult = sendRequest(function(resultCallback){
return result;
});
return movieResult;
};
return new Tmdb();
}
function sendRequest(resultCallback){
var req = https.request(options, function(res) {
var chunks = [];
res.on('data', function (chunk) {
chunks.push(chunk);
});
res.on('end', function(){
var resultBody = Buffer.concat(chunks);
resultCallback(JSON.parse(resultBody.toString()));
});
});
req.write("{}");
req.end();
}
Why do I get a "undefined" on console.log(result);?
I think the problem is the async function calls but how can I return my object here?
via Kumaro
No comments:
Post a Comment