如何控制针对异步操作的http请求
How to control http request that targets Async operation
我正在尝试在登录过程中获取用户数据。
数据存储在 rethinkDb 中。
流程是:
• 请求被路由到控制器(通过 express)
• 控制器选择正确的处理程序
• 处理程序调用 dao.get():
login: function (email, password, res) {
var feedback = daouser.get(email);
if (feedback.success) {
var user = feedback.data;
//Do some validatios...
}
res.status = 200;
res.send(feedback);
},
• dao.get() 代码为:
get: function (email) {
var feedback = new Feedback();
self.app.dbUsers.filter({email: email}).run().then(function (result) {
var user = result[0];
feedback.success = true;
feedback.data = user;
return feedback;
});
}
但由于调用是通过承诺进行的,因此调用实际“Then”函数之前的 dao.get return 并且控制器获得 undefined 反馈…
我的设计有问题…
var feedback = daouser.get(email);
您不能在这里进行同步赋值,因为 .get 是异步的。另外,请注意您没有从 .get 返回任何内容,这就是它未定义的原因。
我会让所有这些成为承诺链。
get: function (email) {
var feedback = new Feedback();
// RETURN is important here, this way .get() return a promise instead of undefined
return self.app.dbUsers.filter({email: email}).run().then(function (result) {
var user = result[0];
feedback.success = true;
feedback.data = user;
return feedback;
});
}
login: function (email, password, res) {
//return the promise again, so login will be chainable too
return daouser.get(email)
// You can chain another then here, because you returned a promise from .get above
// Your then function will be called with the return from the previous then, which is 'feedback'
.then(function(feedback) {
if (feedback.success) {
var user = feedback.data;
//Do some validatios...
}
res.status = 200;
res.send(feedback);
}
},
我正在尝试在登录过程中获取用户数据。 数据存储在 rethinkDb 中。 流程是:
• 请求被路由到控制器(通过 express)
• 控制器选择正确的处理程序
• 处理程序调用 dao.get():
login: function (email, password, res) {
var feedback = daouser.get(email);
if (feedback.success) {
var user = feedback.data;
//Do some validatios...
}
res.status = 200;
res.send(feedback);
},
• dao.get() 代码为:
get: function (email) {
var feedback = new Feedback();
self.app.dbUsers.filter({email: email}).run().then(function (result) {
var user = result[0];
feedback.success = true;
feedback.data = user;
return feedback;
});
}
但由于调用是通过承诺进行的,因此调用实际“Then”函数之前的 dao.get return 并且控制器获得 undefined 反馈…
我的设计有问题…
var feedback = daouser.get(email);
您不能在这里进行同步赋值,因为 .get 是异步的。另外,请注意您没有从 .get 返回任何内容,这就是它未定义的原因。 我会让所有这些成为承诺链。
get: function (email) {
var feedback = new Feedback();
// RETURN is important here, this way .get() return a promise instead of undefined
return self.app.dbUsers.filter({email: email}).run().then(function (result) {
var user = result[0];
feedback.success = true;
feedback.data = user;
return feedback;
});
}
login: function (email, password, res) {
//return the promise again, so login will be chainable too
return daouser.get(email)
// You can chain another then here, because you returned a promise from .get above
// Your then function will be called with the return from the previous then, which is 'feedback'
.then(function(feedback) {
if (feedback.success) {
var user = feedback.data;
//Do some validatios...
}
res.status = 200;
res.send(feedback);
}
},