Javascript 使用async/await处理Thunk操作中的捕获
在这里很难弄清楚如何处理异常 容器Javascript 使用async/await处理Thunk操作中的捕获,javascript,reactjs,redux,react-redux,redux-thunk,Javascript,Reactjs,Redux,React Redux,Redux Thunk,在这里很难弄清楚如何处理异常 容器 async handleResetPassword(uuid) { console.log("handleResetPassword invoked!") try { await this.props.resetUserPassword() ...rest of the code Thunk动作 export function resetPasssord() { return async (
async handleResetPassword(uuid) {
console.log("handleResetPassword invoked!")
try {
await this.props.resetUserPassword()
...rest of the code
Thunk动作
export function resetPasssord() {
return async (dispatch, getState) => {
const state = getState()
try {
const response = await UserApi.ResetUserPassword(state.auth.token)
if(response && response.body){
dispatch(UserActions.resetPasswordReceived)
}
dispatch(UserActions.resetPasswordFail)
}
catch(err) {
//what do I do here? How should I resolve this?
dispatch(UserActions.resetPasswordFail)
}
}
}
以及如何处理相关API的捕获。如上所示,thunk操作在此处调用Api:
用户API(使用超级代理):
以下是我在头脑风暴时的想法:
- 需要从catch中的API调用返回某种承诺,以便wait可以在thunk操作中解析
- 需要从catch中的thunk action返回某种承诺,以便容器能够解决拒绝问题
- 我是否为try-catch中的错误分派操作
const ResetUserPassword = async (token) => {
try{
return await request
.post(`${endpoint}/users/recover-password`)
.set({ Authorization: `Bearer ${token}` })
.accept('application/json')
}
catch(err) {
console.log(err)
//how handle this?
}
}