我知道这个问题是常见的。我正在使用es6承诺,并且有多个层次。
在运行时,当我没有捕获一个承诺时,在我的控制台上会出现未捕获的(承诺)
。但事实是,我在我的代码中较低的位置确实捕获了它。
简化的快速示例:
LoginApi.js
var loginDaoCall = loginDao.login(username, password);
loginDaoCall
.then(function (res) {
store.dispatch(loginSuccess());
log.log("[loginApi.login] END");
})
.catch(function (err) {
store.dispatch(loginFail());
errorUtils.dispatchErrorWithTimeout(errorLogin);
log.log(err);
});
return loginDaoCall;
loginContainer.js
loginApi.login(user, password).then(() => {
// Change here instead of in render so the user can go back to login page
this.props.history.push(baseUrlRouter + "test");
}); // <- Error here cause I don't CATCH the promise, but I do catch it in my loginapi.js
我知道如果什么都不做就会触发此错误,但是嘿,我也可以在我的API层中进行历史推送,但这不是它的职责。
有没有办法避免控制台中的错误?我甚至考虑保留它。