Home > Mobile >  Unhandled promise rejection warning in React Native
Unhandled promise rejection warning in React Native

Time:06-06

I'm getting the following warning while executing this function:

const requestCameraPermission = useCallback(async () => {
    console.log('Requesting camera permission...')
    const permission = await CameraComponent.requestCameraPermission()
    console.log(`Camera permission status: ${permission}`)

    if (permission === 'denied') await Linking.openSettings()
    setCameraPermissionStatus(permission)
  }, [])

CodePudding user response:

You have to enclose your function with a try catch to handle a promise with async await.

const requestCameraPermission = useCallback(async () => {
  try {
    console.log('Requesting camera permission...')
    const permission = await CameraComponent.requestCameraPermission()
    console.log(`Camera permission status: ${permission}`)

    if (permission === 'denied') await Linking.openSettings()
    setCameraPermissionStatus(permission)
  } catch (error) {
    console.log(error);
  }
}, [])
  • Related