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);
}
};
catchnever runs beforetry. Possibly explanations for what you see include thatonSignupis called multiple times or that your toaster displays messages in the wrong order. So do some debugging what thevaluespassed to each call are, and whaterroris thrown.