proyecto26 / react-native-inappbrowser

📱InAppBrowser for React Native (Android & iOS) 🤘
https://www.npmjs.com/package/react-native-inappbrowser-reborn
MIT License
1.31k stars 223 forks source link

Alert.alert() not working (most of the time) in catch block on android #339

Closed Adnan-Bacic closed 2 years ago

Adnan-Bacic commented 2 years ago

Which platform(s) does your issue occur on?

Please, provide the following version numbers that your issue occurs with:

Please, tell us how to recreate the issue in as much detail as possible.

Describe the steps to reproduce it.

  1. write a try/catch with and alert() in the catch
  2. Put an invalid url to try. i have tried "test" or empty string "" or "google.invalid". it doesnt alert.
  3. click to open the link on android. no alert is shown. other code written after the alert is run, such as console.log(). even custom functions afterwards work.

SOMETIMES it alerts, but rarely, and then it only does it once. after it has been dismissed it wont appear again.

alerts works fine otherwise.

Is there any code involved?

this is basically just the code from the example, i just removed all the options and put an invalid url:

async openLink() {
    try {
      const url = 'test'
      if (await InAppBrowser.isAvailable()) {
        const result = await InAppBrowser.open(url)
        Alert.alert(JSON.stringify(result))
      }
      else Linking.openURL(url)
    } catch (error) {
      console.log(error)
      Alert.alert(error.message)
      console.log('post alert')
    }
  }
jdnichollsc commented 2 years ago

Try adding a delay before showing the alert, something like this:

async sleep(timeout: number) {
  return new Promise(resolve => setTimeout(resolve, timeout));
}

async openLink() {
    try {
      const url = 'test'
      if (await InAppBrowser.isAvailable()) {
        const result = await InAppBrowser.open(url);
        await this.sleep(800);
        Alert.alert(JSON.stringify(result))
      }
      else Linking.openURL(url)
    } catch (error) {
      Alert.alert(error.message)
      console.log('post alert')
    }

During my tests I found race conditions like this scenario with these native components, please try and let me know! 👍