javascript - 异步等待或 .then() 为 redux 操作返回 undefined
问题描述
我正在这样做:await dispatch(myfunction(arg1))
如果我添加.then()
或
const x = await dispatch(myfunction(arg1))
console.log(x)
两者都以未定义的形式返回...
但是当我在浏览器中查看响应时,我得到了我期望的数据
有任何想法吗?
动作创建者只是通过一些中间件并调用一些动作,但不知道为什么它是未定义的
解决方案
Dispatch 不返回承诺。它只是触发更新发生。如果您需要使用 Promise,请考虑使用 redux-thunk,然后执行以下操作:
let thunkAction = async (args1) => {
let res = await myFunction(args1);
dispatch(someUpdate(res));
}
推荐阅读
- python - 如何使 Django UpdateView 仅在第一次工作?
- python - 如何将图像的背景颜色与 Pygame 的背景颜色匹配?
- javascript - 我无法使用在 vanilla JavaScript 中创建的自定义函数切换类
- neo4j - 尝试批量合并相同的节点
- python - 如何存储 Observer on_completed 结果
- php - 使用 php 添加原生 ACF 字段
- rust - 如何使用 Rust 从文件中获取 DOM?
- https - HTTPS 上 GCP 上的静态网站无法正常工作
- tensorflow - 循环神经网络,使用更新的 Tensorflow 1.14 进行时间序列预测
- python - 调用 ASK SDK for Python 的修饰请求处理程序