简体   繁体   English

使用异步来反应redux地理位置并等待

[英]React redux geolocation with async and await

so I am trying to pull the lng and lat using geolocation with async and await. 所以我试图通过异步和等待使用地理定位来拉动lng和lat。 For some reason, it didn't wait for the promise to return before passes it to the reducer. 由于某种原因,它没有等到诺言返回之前才将其传递给减速器。 I am getting another promise. 我得到另一个诺言。

I expected to use await to get the result back from the promise before moving on to different parts of the program. 我希望在进入程序的不同部分之前使用await从诺言中获得结果。

geolocation in reducer gets called before the result of the promise is returned. 在返回promise结果之前,调用reducer中的geolocation。

在此处输入图片说明 在此处输入图片说明

在此处输入图片说明

You've misunderstood the purpose of a Promise . 您误解了Promise的目的。

When you create a promise, it does not pause execution of the code. 创建承诺时,它不会暂停代码的执行。 In fact, the entire reason to use a promise is when you want the code to continue it's execution without waiting for a response. 实际上,使用promise的全部原因是当您希望代码继续执行而无需等待响应时。

There exist a number of middleware solutions to solving this problem. 存在许多解决此问题的中间件解决方案。 ReduxThunk is probably the most popular, but if you prefer a more async-await methodology you can also also tyr redux-asyncy-await . ReduxThunk可能是最受欢迎的,但是如果您更喜欢异步等待方法,您还可以输入redux-asyncy-await

A middleware solution such as these is required to handle async data calls. 需要这样的中间件解决方案来处理异步数据调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM