0

How to return a variable from a httpreq function?

function getTrailerYoutube(movieName){
    var getVideoList = 'https://www.googleapis.com/youtube/v3/search?part=snippet&q=' 
       + movieName + '+Trailer&type=video&videoDuration=short&key=<mykey>'

    httpreq.get(getVideoList, function (err, res){
      if (err) return console.log(err);
       var moviefront = JSON.parse(movieback);
       var result = moviefront.video.videoId
       return result
    });

}

console.log(result); //undefined
I although tried a second function and a global variable, but I didn't got a result.
WillardSolutions
  • 2,316
  • 4
  • 28
  • 38
Ben
  • 1
  • 3
  • The callback function (`function(err, res){}`) is a callback for an asynchronous action. move `console.log(result);` to inside the callback function and you'll see the result. – Stephen Last May 05 '16 at 16:04
  • Is `httpreq.get` a third party library function? – Mark C. May 05 '16 at 16:04
  • @stephenLast Thanks, i know, but it need the result in another function. @ MarkC. Yes, it a npm package [link](https://www.npmjs.com/package/httpreq) – Ben May 05 '16 at 16:07

0 Answers0