redux - 有没有更好的方法在捕获拒绝时链接异步 thunk?
问题描述
由于创建的 thunkcreateAsyncThunk
将始终返回已解决的 promise。有没有比unwrapResult
每次都添加以捕获拒绝更好的方法来处理链接的重击?
const fetchUsers = createAsyncThunk('users/fetch', myService.fetchUsers);
const updateUser = createAsyncThunk('users/update', myService.updateUser);
export const updateAndFetch = values => async dispatch => {
const result = await dispatch(updateUser(values));
const unwrapped = unwrapResult(result); // required to see if first update was rejected
return dispatch(fetchUsers());
}
解决方案
并不真地。
你也可以这样写
export const updateAndFetch = values => async dispatch => {
const result = await dispatch(updateUser(values)).then(unwrapResult);
return dispatch(fetchUsers());
}
或者
export const updateAndFetch = values => async dispatch => {
const result = await dispatch(updateUser(values));
if (updateUser.fulfilled.match(result)) {
return dispatch(fetchUsers());
}
}
但在某些时候你必须做那个检查。
推荐阅读
- ruby-on-rails - 如何在 Rails 6 ActiveStorage 中附加带有扩展名的文件名?
- node.js - 在 Mongo DB 中用另一个对象替换对象数组
- sql - 以行而不是单行显示 SQL 结果
- asp.net-core - DisableRequestSizeLimit 和 RequestFormLimits 在服务器上不起作用
- r - 在最右列和最底行创建具有平均值的热图
- node.js - 无法连接到 selenium chrome 独立驱动程序
- angular - Firebase 身份验证仍然会在控制台中引发错误,即使我发现了它
- reactjs - 无法使用反应上下文值
- google-bigquery - 如何在 Big Query 中将表从一个数据集移动到另一个数据集
- django - HttpResponseRedirect 有额外的路径并失败