[英]using express route handlers and promises -what are the best practices?
I'm currently working on a web project and I'm trying to understand Node.js and express. 我目前正在研究一个Web项目,并且试图理解Node.js和表达。 I know about that for achiving asynchronous easy to maintain code we should use promises , but I'm confused about the following scenario: I have a route handler for REST API , should I return a promise ?
我知道要实现异步且易于维护的代码,我们应该使用Promise,但是我对以下情况感到困惑:我有一个REST API的路由处理程序,我应该返回Promise吗? even if there is no Async work done there ?
即使在那里没有完成异步工作? ie :
即:
dummy_router.post("/", passport.authenticationMiddleware(), (req, res) => {
return new Promise((fulfill, reject) => {
var result = some_simple_functionality();
if (result) {
fulfill(result)
} else {
reject(err)
}
}).catch(function (error) {
res.status(400).send('400: failed t :: ' + error + '\n');
});
});
also second question on same issue . 同样是第二个问题。 if I am performing some async tasks and have promises containing them then is this a good practice ?
如果我正在执行一些异步任务并承诺包含它们,那么这是一个好习惯吗?
dummy_router.post("/", passport.authenticationMiddleware(), (req, res) => {
some_promise().then((result) => {
}, (err) => {
}).catch(function (error) {
res.status(400).send('400: failed t :: ' + error + '\n');
});
});
Turning my comments into an answer: 将我的评论变成答案:
Your first example is manufacturing a promise when none is needed. 您的第一个示例是在不需要时实现承诺。 There is no reason to wrap synchronous code in a promise.
没有理由将同步代码包装在Promise中。 That just adds complexity that is not needed and adds no benefit.
这只会增加不必要的复杂性,也不会带来任何好处。 Express does not do anything with the return value from a request handler so there is no use in returning a promise from that at all.
Express对请求处理程序的返回值不做任何事情,因此根本没有用它返回承诺。
Your second example appears to be using an actual asynchronous operation that returns a promise. 您的第二个示例似乎正在使用返回诺言的实际异步操作。 That looks generally correct, but it is unclear why you have both an error handler on your .then() and a .catch().
这看起来通常是正确的,但尚不清楚为什么在.then()和.catch()上同时具有错误处理程序。 Pick one or the other and be aware that a reject handler that does not rethrow or return a rejected promise will change the promise into a fulfilled promise (overriding the rejection).
选择一个或另一个,请注意,不重新抛出或返回被拒绝的承诺的拒绝处理程序会将承诺更改为已实现的承诺(覆盖拒绝)。
So, when you do this: 因此,当您这样做时:
some_promise().then((result) => {
}, (err) => {
// unless you rethrow here, this will "handle" the error
// and the promise will become fulfilled, not rejected
}).catch(function (error) {
res.status(400).send('400: failed t :: ' + error + '\n');
});
The (err) => {}
handler is causing your promise rejection to get eaten unless you rethrow or return a rejected promise from that. (err) => {}
处理程序会导致您的诺言拒绝被吃掉,除非您从中抛出或返回被拒绝的诺言。
So, probably all you want is this: 因此,可能您想要的就是:
some_promise().then((result) => {
// code to process result here
res.send(...);
}).catch(function (error) {
res.status(400).send('400: failed t :: ' + error + '\n');
});
There is rarely ever a reason to use both a .then()
reject handler (the 2nd argument to .then()
and a subsequent .catch()
handler. 很少有过一个理由,同时使用
.then()
拒绝处理(第2参数.then()
和随后的.catch()
处理程序。
Is there justification to use both the rejection handler and the catch() then?
有理由同时使用拒绝处理程序和catch()吗?
No. There are a few uncommon situations why you might want both, but in this case (and in most cases), just having the .catch()
handler is all you need. 不会。在几种不常见的情况下,您可能两者都想要,但在这种情况下(大多数情况下),只需要
.catch()
处理函数即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.