Home > Back-end >  Function call returns Promise {_U: 0, _V: 0, _W: null, _X: null} instead of true or false
Function call returns Promise {_U: 0, _V: 0, _W: null, _X: null} instead of true or false

Time:11-06

I am trying to call my NetInfo function like this on a separate file:

import NetInfo from '@react-native-community/netinfo';

export default class checkNetwork {
  static async isNetworkAvailable() {
    const response = await NetInfo.fetch();
    return response.isConnected;
  }
}

On my redux saga file:

const connectionStatus = async () => {
  await checkNetwork.isNetworkAvailable();
};
const isConnected = connectionStatus();
console.log('status of network >>', isConnected);

Instead of true or false this returned a promise:

status of network >> Promise {_U: 0, _V: 0, _W: null, _X: null}

How do I fix this to return a boolean?

CodePudding user response:

Async function always returns a promise and to resolve it you need to await it, so simply add await in front of connectionStatus function.

const isConnected = await connectionStatus();

If you are using react saga ( generators ) to achieve it, you can use yield instead of await.

const isConnected = yield connectionStatus();
  • Related