React Native Axios Call Inside Function

I tried to put POST by AXIOS function inside another function to reuse it multiple times. but unfortunately It doesn't work as I expected (which was working using FETCH),

AXIOS CALL JS:

export async function post(apiRoute, body) {

    try {


        body = JSON.stringify(body);
        axios.post(apiRoute, body, httpOptions)
             .then((res)=>{
                console.log(res.data);                // I have data here
                return res.data;
             });
    }
    catch (err) {
        console.log(err);
        return err;
    }
}

And the caller is:

    async DoLogin(userName, password) {
        
        var data = await post(url,{     // post is axios method defined above in another file
            UserName: userName,
            Password: password
        });


        return data;                // I have nothing here
    }

And the problem is I got undefined in DoLogin but got data inside POST method, It seems the problem is related to timing, axions returns a promise which is not possible to read in DoLogin, how I can do this? I did this via FETCH and it works perfectly.



Read more here: https://stackoverflow.com/questions/64405385/react-native-axios-call-inside-function

Content Attribution

This content was originally published by Mertez at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: