![](/img/trans.png)
[英]Redux Toolkit w/ TS: what is the recommended approach for dispatching two consecutive async thunk actions
[英]Redux Toolkit: lifecycle action types doesn't sync up when dispatching multiple async thunk actions in one thunk
我有一个异步 thunk 操作来编排多个这样的异步 thunk
const someAction = createAsyncThunk(
'my/someAction',
async () => {...}
)
const someAction2 = createAsyncThunk(
'my/someAction2',
async () => {...}
)
const generatedThunk = createAsyncThunk(
'my/thunk',
async (arg, thunkApi) => {
thunkApi.dispatch(someAction());
thunkApi.dispatch(someAction2());
}
)
我发现异步 thunk 之一,即someAction
和someAction
被拒绝, my/thunk
仍然会得到满足。 当任何一个my/thunk
拒绝时,如何让my/thunk
被拒绝?
createAsyncThunk
始终捕获错误,以便我们避免在您的应用程序中出现“未捕获的承诺拒绝”消息。
您需要自己检查每一个的结果,并相应地返回一个被拒绝的承诺。
另外,请注意,按照当前编写的方式,您的代码将同时运行这两个 thunk。 没关系,只要确保它是您真正想要的 - 要串行运行它们,您需要await
它们。
这里的一种选择是:
return Promise.all([
thunkApi.dispatch(someAction()).then(unwrapResult),
thunkApi.dispatch(someAction2()).then(unwrapResult)
]);
那将:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.