简体   繁体   English

反应原生:异步,等待,获取

[英]React native: async, await, fetch

In my app, I'd like to perform an HTTP request, wait for the request to be completed, and show the result在我的应用程序中,我想执行一个 HTTP 请求,等待请求完成,然后显示结果

function HomeScreen({ navigation, route }) {

....

console.log("Try to decode");

const result = GeocodingUtils.getAdressFromCoordinates(myLocation);

console.log("Get Address OK");

<Text>{result}</Text>

And this is my getAdressFromCoordinates method这是我的 getAdressFromCoordinates 方法

export default class GeocodingUtils {

static async getAdressFromCoordinates(location) {

    try {
        const response = await fetch("valid_url");
        const responseJson = await response.json();

        .....

        console.log(street + " " + streetNumber);

        return street + " " + streetNumber;

    } catch (error) {
        console.log(error);
    }
}

I taught that the console output would have been something like:我教过控制台输出应该是这样的:

Try to decode尝试解码

St. Mary street圣玛丽街

Get Address OK获取地址确定

but that's the true output:但这是真正的输出:

Try to decode尝试解码

Get Address OK获取地址确定

/ print a promise object / /打印一个承诺对象/

So I have two problems:所以我有两个问题:

the first one is that my geocode function should "stop" the code flow execution until its end the second is that I'd like to print the result of my funciont in a Text component第一个是我的地理编码功能应该“停止”代码流执行直到它结束第二个是我想在文本组件中打印我的功能的结果

Ty in advance提前TY

The problem you 're having is that you 're calling an async method and not waiting for it to respond before storing it in the result const.您遇到的问题是您正在调用异步方法并且在将其存储在结果常量中之前没有等待它响应。

That's why you 're getting a Promise back.这就是为什么你会得到一个 Promise 回来。

try this :尝试这个 :

let result;
GeocodingUtils.getAdressFromCoordinates(myLocation).then(r => result = r);

or even better define your function as async:甚至更好地将您的函数定义为异步:

var HomeScreen = async({ navigation, route }) => {

....

console.log("Try to decode");

const result = await GeocodingUtils.getAdressFromCoordinates(myLocation);

console.log("Get Address OK");

<Text>{result}</Text>

The solution I've found is to use the useEffect hook, like我找到的解决方案是使用 useEffect 钩子,比如
kevinSpaceyIsKeyserSöze said. kevinSpaceyIsKeyserSöze 说。

useEffect(() => {

    (async () => {

        ....

        let addressString = await GeocodingUtils.getAdressFromCoordinates(
            latitude, longitude
        );

    })();
}, []);

You need to await the GeocodingUtils.getAdressFromCoordinates result:您需要await GeocodingUtils.getAdressFromCoordinates结果:

function HomeScreen({ navigation, route }) {

....

console.log("Try to decode");

const result = await GeocodingUtils.getAdressFromCoordinates(myLocation);

console.log("Get Address OK");

<Text>{result}</Text>

You can also use promises:您还可以使用承诺:

function HomeScreen({ navigation, route }) {

....

console.log("Try to decode");

GeocodingUtils.getAdressFromCoordinates(myLocation).then(function(result){
console.log("Get Address OK")
<Text>{result}</Text>
})

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

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