Skip to content Skip to sidebar Skip to footer

Catching Errors From One Function Inside Another Javascript React

I have two functions, login (in fileB.js): export const login = async (data) => { try { const response = await auth.login(data); return response; } catch (e) { r

Solution 1:

You are converting promise rejection into promise fulfilment by returning an error object.

Retuning a non-promise value from the catch block will fulfil the promise returned by the login function with the return value of the catch block.

To reject the promise returned by the login function:

  • Re-throw the error caught by the catch block, or

  • Remove the try-catch block from the login function and let the calling code handle the error.

login function could be re-written as:

export const login = (data) => {
    return auth.login(data);
};

I suggest that you choose the second option and re-write the login function as shown above. There is no need for a catch block that just re-throws the error.

Post a Comment for "Catching Errors From One Function Inside Another Javascript React"