打破这条链条的正确方法是什么?例如,如果我在catch中,我不希望执行以下两个then
。
我曾经把catch
放在最后,但后来把它移到了前面,觉得这可能会有帮助。事实并非如此。
return axios
.post(url, data)
.catch(error => {
const message = getBannerTextFailureMessage(liability, toStatus)
dispatch(showBanner(message, bannerConstants.typeError))
})
.then(response => {
return dispatch(updateLiabilitiesComplete())
})
.then(response => {
const message = getBannerTextSuccessMessage(liability, toStatus)
dispatch(showBanner(message, bannerConstants.typeSuccess))
})
发布于 2018-07-14 02:19:42
.catch
捕获promise链之前的任何部分,并在.catch
未捕获后离开后面的.then
而不使用reject函数(除非有另一个.catch
)。这意味着当.catch
在链的末尾时,你的链可能在某个.then
中失败了,当你没有捕获它时,可能会向控制台抛出promise错误(紧跟在.post
之后的.catch
)。
只需删除(临时) .catch
,并从控制台日志中找出它出现故障的部分。
发布于 2018-07-14 02:34:19
在解析promise之后,catch
用于捕获来自任何then
块的任何错误。因此,如果您将一个catch
块放在then
之后,将会捕获前一个then
块中可能出现的第一个错误。所以在你使用过的地方使用catch是没有意义的,而且第一个then
块在解析后会从promise中获取返回值。
在catch块中:
执行console.log(error)
以找出您得到的错误。
此外,要从Promise解析函数中检索值,请使用eventlisteners
(断开链接)。
https://stackoverflow.com/questions/51330377
复制相似问题