有没有更好的方法在捕获拒绝时链接异步 thunk?

Is there a better way of chaining async thunks while catching rejections?

自从使用 createAsyncThunk will always return a resolved promise 创建了一个 thunk。有没有比每次都添加 unwrapResult 以捕获拒绝更好的处理链式 thunk 的方法?

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());
  }
}

但在某些时候您必须进行检查。