Reputation: 3011
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.
Client.js
$.ajax('localhost/Server').done(function(){
});
Server.js
function cb(){
// do stuff
}
// ajax request is going here
function Server(req, res) {
GetTheModelFromTheDbInAsyncWay(function(cb){
cb();
});
}
function GetTheModelFromTheDbInAsyncWay(cb) {
//doing stuff to the db e.g getting the result of a query
// ...
cb(result);
}
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 promised
.
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
Upvotes: 0
Views: 60
Reputation: 1074475
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`
});
}
Upvotes: 2