[英]Add reject/resolve to catch/success
I use the following code and I wonder for best practise usage if I should add reject to this promise inside the catch? 我使用以下代码,并且我想为最佳实践使用是否应该在catch中将此拒绝添加拒绝?
run: function (req, res) {
if (req) {
return this._un(req).then(function() {
return proce.restart().then(function() {
return res.status(200).end("sucess");
//Here should I use reslove
});
}).catch(function(err) {
return res.status(500).send("error: " + err);
//Here should I use reject???
});
}
else {
return new Promise(function(resolve, reject) {
reject("No application content found");
});
}
}
};
You don't "add reject" to a promise. 您不会在承诺中“添加拒绝”。 A promise is either unsettled, or settled (resolved/rejected).
承诺未结算或已结算(已解决/已拒绝)。
If req
is provided, your code currently returns a promise that will be resolved with the return value of end
(if the restart was successful) or the return value of send
(if it wasn't), which I believe in both cases is the response object itself ( res
). 如果提供了
req
,则您的代码当前会返回一个promise,它将以end
的返回值(如果重新启动成功)或send
的返回值(如果没有成功)来解析,我认为在两种情况下都是响应对象本身( res
)。
If you want the caller to be aware of whether the restart was successful, then yes, you want to reject the promise instead; 如果您希望呼叫者知道重启是否成功,那么可以,您想拒绝承诺;相反,您可以拒绝承诺。 with ES2015 promises you can do that by throwing in
catch
and I assume Bluebird is similar: ES2015承诺您可以通过抛出
catch
来做到这一点,并且我认为Bluebird与之类似:
.catch(function(err) {
res.status(500).send("error: " + err);
throw err; // Or `throw new Error(err);`, it depends on what `err` is and your convention
})
...or by using Bluebird's Promise.reject
(which is also ES2015-compatible): ...或通过使用Bluebird的
Promise.reject
(也与ES2015兼容):
.catch(function(err) {
res.status(500).send("error: " + err);
return Promise.reject(err);
})
If you don't want the caller to be aware of whether the restart was successful, then don't. 如果您不希望调用者知道重新启动是否成功,则不要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.