How do I bubble up an error from react native?

I have one functional component where I call a methd from another .js file. The method inside the .js file throws an error (which is OK, for my testing purposes), but I want to bubble this error up to the method inside the functional component. How do I do that?

import {
  pickImageAsync,
} from "./someFile";

export default class Component1 extends React.Component<any> {
  render() {
    const {  sendError } = this.props;

    function pickImage() {
      try {
        //calling from another file
        pickImageAsync(onSend);
      } catch (error) {
        //I dont reach this part of the file
        console.log("I am catching error: ", error);
        sendImageError(true);
      }
    }

In my file someFile.js:

export async function pickImageAsync(onSend) {
  if (await getImagePickerPermissionAsync()) {
    let result;
    try {
      //with the way I test it this method throws the error, which I want to bubble up
      result = await ImagePicker.launchImageLibraryAsync({
        mediaTypes: ImagePicker.MediaTypeOptions.Images,
        quality: 1,
      });
      //with the way I test it, I dont see any result, which is OK for my testing purposes
      console.log(result);
       // I dont reach this peace of code which is fine
       if (!result.cancelled) {
             onSend([{ image: result.uri }]);
       }
    } catch (error) {
      //I am catching the error and I see it on the console, but how do I bubble it up?
      console.log("could not select image: ", error);
    }
  }
}

Answer

You need to throw this error (throw error). This will bubble it up in the call stack until someone is catching it (or not and you will see an error in console).

Look at the last line I added:

export async function pickImageAsync(onSend) {
  if (await getImagePickerPermissionAsync()) {
    let result;
    try {
      //with the way I test it this method throws the error, which I want to bubble up
      result = await ImagePicker.launchImageLibraryAsync({
        mediaTypes: ImagePicker.MediaTypeOptions.Images,
        quality: 1,
      });
      //with the way I test it, I dont see any result, which is OK for my testing purposes
      console.log(result);
       // I dont reach this peace of code which is fine
       if (!result.cancelled) {
             onSend([{ image: result.uri }]);
       }
    } catch (error) {
      //I am catching the error and I see it on the console, but how do I bubble it up?
      console.log("could not select image: ", error);
      throw error;
    }
  }
}