I can't seem to make this work but I want to return true every-time the function executes successfully, which in this case is "Changing the password".
async function ChangePassword(data) { const auth = getAuth(); let res = false; const credential = EmailAuthProvider.credential( auth.currentUser.email, data.oldPassword ); reauthenticateWithCredential(auth.currentUser, credential) .then(() => { updatePassword(auth.currentUser, data.password) .then(() => { toast.success("Password changed successfully"); res = true; console.log("res ",res); }) .catch((error) => { toast.error(error.message); }); }) .catch((error) => { toast.error(error.message); }); return res; } The res variable when called by other functions always results in false even though I see the toast message "Password changed successfully".
async function changePassword() { if (password === "" || confirmPassword === "" || oldPassword === "") { toast.error("Please fill all the fields"); } else if (password !== confirmPassword) { toast.error("Passwords do not match"); } else { let data = { oldPassword, password }; await ChangePassword(data).then((res) => { if (res === true) { setChangePasswordModal(false); setOpen(false); } }); } } The if condition in above code never executes because res is always false. I know it has something to do with async await but can't figure out how to make it work
Promisewith synchronous code +asyncis going to setup you up for a bad time.Promise.thenand alike, orasyncbut please NOT both.