异步/等待不等待响应

时间:2020-03-05 12:21:00

标签: react-native react-native-android

我正在使用POST方法请求一个api。但是使用await调用RemoteRequest函数之后。它不是在等待响应返回。通过将register.js放入status = undefind,直接执行console.log中剩余的代码。

register.js

const DeviceUniqueId = DeviceInfo.getUniqueId();
const requestBody = { phone: phone, username: username };
const status = await RemoteRequest(URLs.APP_REGISTER, 
'POST', DeviceUniqueId, requestBody);

console.log('status====>', status);
this.setState({
    loading : false
});

remotereuqest.js

export const RemoteRequest = async (url, method, DeviceUniqueId, requestbody) => {
    console.log(url, method, DeviceUniqueId, requestbody);

    NetInfo.fetch().then((state) => {
        if (state.isConnected) {
            fetch(url, {
                method  : method,
                headers : {
                    Accept           : 'application/json',
                    'Content-Type'   : 'application/json',
                    DEVICEID         : DeviceUniqueId,
                    'Accept-Charset' : 'utf-8'
                },
                body    : JSON.stringify(requestbody)
            })
                .then((response) => {
                    console.log('reponse=====>', response);
                    return response.json();
                })
                .then((responseData) => {
                    console.log(responseData);
                    if (responseData.status == 'OK' && responseData.code == 200) {
                        return responseData.code;
                    }
                    return null;
                })
                .catch((error) => {
                    console.log(error);
                    if (error.message == 'Network request failed') {
                        showMessage({
                            floating        : true,
                            message         : 'Connection error',
                            description     : 'you have no Internet Connection',
                            type            : 'alert',
                            backgroundColor : 'red'
                        });
                        return null; //503
                    }
                    else {
                        showMessage({
                            floating        : true,
                            message         : 'Internal Server Error',
                            description     : 'please try again after some time',
                            type            : 'alert',
                            backgroundColor : 'red'
                        });
                        throw error;
                        return null;
                    }
                })
                .done();
        }
        else {
            showMessage({
                floating        : true,
                message         : 'Connection error',
                description     : 'you have no Internet Connection',
                type            : 'alert',
                backgroundColor : 'red'
            });
            return null; //503
        }
    });
};

2 个答案:

答案 0 :(得分:0)

您需要等待函数中的所有promise,否则它们仍将异步执行。像这样:

await NetInfo.fetch().then( async (state) => {
    if (state.isConnected) {
        await fetch(url, { 
        ...
        }
    }
});

答案 1 :(得分:0)

使用.then()时,代码将立即执行,因此,您应该等待响应,然后在没有.then()-s的情况下进行工作。

const state = await NetInfo.fetch();
if (state.isConnected) {
  const response = fetch(url, ...);
  console.log(response);
  ...
}