Warm tip: This article is reproduced from serverfault.com, please click

Login Displaying unwanted Alert

发布于 2020-11-30 05:35:43
    function signUp(){
    
    var email = document.getElementById("email");
    var password = document.getElementById("password");
    
    const promise = auth.createUserWithEmailAndPassword(email.value, password.value);

            promise.catch(e => alert(e.message));
    
    
    
        alert("Signed Up");


}

I have this code here that when a valid email and password are entered the user presses a button and signs the user up. But when the user enters invalid data the alert("Signed Up") displays along with the error message, is there any way to avoid that?

Questioner
nova3333
Viewed
11
danh 2020-11-30 13:40:32

As written, the "Signed Up" alert runs immediately, before the createUser is complete, whether or not it succeeds. The way to use promises is to put the code that should execute after successful completion in a then block, and the error handling -- as you have it -- in a catch...

const promise = auth.createUserWithEmailAndPassword(email.value, password.value);
promise.then(() => alert("Signed Up")).catch(e => alert(e.message));