【问题标题】:Do I need to await an async Redux Thunk (not Promise)?我需要等待异步 Redux Thunk(不是 Promise)吗?
【发布时间】:2021-09-25 14:33:43
【问题描述】:

假设这是我的 thunk:

const thunk = async (dispatch,getState) =>{ 'some dispatch and await code'}

所以当我想发送这个 thunk 时,我应该使用 await dispatch(thunk) 还是只使用 dispatch(thunk)

从 Redux-thunk 和 Redux 的源代码来看,我认为我应该使用 await dispatch(thunk),因为 redux-thunk 不会为我添加“等待”。但是如果我不使用 await 会发生什么(它只是返回一个待处理的 Promise),thunk 中的代码(从 Api 调度和拉取等)仍然会被执行吗?

谢谢

【问题讨论】:

  • 它仍然会被执行,但它不再是异步的,因为你没有等待结果

标签: redux react-redux redux-thunk


【解决方案1】:

这在很大程度上取决于您以后是否想做某事。

如果您只想发送该 thunk 并让它运行,您可以发送它。 您的下一行代码将在 thunk 启动后立即运行,而不是等待它完成。

或者你可以选择await它,那么你的下一行代码只会在thunk完成或失败后继续运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-23
    • 2019-03-02
    • 2017-06-15
    • 2016-10-24
    • 1970-01-01
    • 1970-01-01
    • 2020-03-24
    • 2017-05-17
    相关资源
    最近更新 更多