我想从我的nodejs数据库中检索一些数据。检索这些数据是一个异步请求,因为我们和ajax请求一样。
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);
}
我需要使用什么技术来检索异步ajax请求中的aync服务器请求?我想它会像promised
一样。但是如何将它传递回ajax请求,因为db请求本身就是异步的。
希望我能说清楚
发布于 2016-10-23 16:29:53
您是,调用是从GetTheModelFromTheDbInAsyncWay
收到的参数,就好像它是一个函数一样。您应该使用它(例如,通过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`
});
}
https://stackoverflow.com/questions/40205104
复制相似问题