Juan Jo Juan Jo - 12 days ago 5
Node.js Question

Node Server return Promise

I am a beginner and can hardly understand promises or callbacks,how can I return the data below so my home server can respond to requests
thanks for any help

else if(requests["mode"]==="onlineVideoQuery"){
var title = requests["title"].replace(/\+/g," ")

imdb.get(title).then(function(data) {
console.log(data)
//I need to get this data above for response.end(data)
});

response.end("how can I return the data above to this response")
}

Answer

You can call the response.end in the then function:

...
else if (requests["mode"] === "onlineVideoQuery") {
  var title = requests["title"].replace(/\+/g, " ")

  imdb.get(title).then(function(data) {
    response.end(data)
  });
}

Make sure call response.end once por request, and to don't mix async code with sync code, that could generate a race condition.