I'm experiencing an issue where the catch block in my signup process is running before the try block. When I attempt to sign up, the catch block displays the error message before the success message is shown. This behavior is unexpected, as I would expect the try block to execute first.
Here is the relevant code snippet:
const onSignup = async (values: any) => {
try {
setLoading(true);
const response = await axios.post(`/api/users/signup`, values);
const responseData = response.data;
if (!responseData.error) {
toast.success("Signup success", successState);
router.push("/login");
} else {
if (responseData.error === "User already exists") {
toast.error("User already exists", errorState);
}
}
} catch (error: any) {
toast.error(
"An error occurred during sign in. Please try again later.",
errorState
);
} finally {
setLoading(false);
}
};