xhallix xhallix - 1 year ago 64
Ajax Question

How to handle an ajax response which is asynchronous

I want to retrieve some data from my database in nodejs.
Retrieving those data is an async request, as we well as the ajax request.





function cb(){
// do stuff

// ajax request is going here
function Server(req, res) {

function GetTheModelFromTheDbInAsyncWay(cb) {
//doing stuff to the db e.g getting the result of a query
// ...

What technique do I need to use, to retrieve the aync server request in my async ajax request?
I think it will be something like
But how can I pass it back to my ajax request since, the db request is async by itself

Hope I was able to make this clear

Answer Source

You're calling the argument you receive from GetTheModelFromTheDbInAsyncWay as though it were a function. Presumably it isn't. You should be using it (for instance, sending it or information derived from it via res.send):

// ajax request is going here
function Server(req, res) {
  GetTheModelFromTheDbInAsyncWay(function(data){ // Not `cb`, `data`
     // Use `data` here to produce the response you send via `res`
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download