首页 > 解决方案 > 异步等待或 .then() 为 redux 操作返回 undefined

问题描述

我正在这样做:await dispatch(myfunction(arg1))如果我添加.then()

const x = await dispatch(myfunction(arg1)) 
console.log(x)

两者都以未定义的形式返回...

但是当我在浏览器中查看响应时,我得到了我期望的数据

有任何想法吗?

动作创建者只是通过一些中间件并调用一些动作,但不知道为什么它是未定义的

标签: javascriptreactjsreduxdispatch

解决方案


Dispatch 不返回承诺。它只是触发更新发生。如果您需要使用 Promise,请考虑使用 redux-thunk,然后执行以下操作:

let thunkAction = async (args1) => {
   let res = await myFunction(args1);
   dispatch(someUpdate(res));
}


推荐阅读