简体   繁体   中英

How do I make execution stop on promise rejection?

I am trying to benefit from promises instead of all these nested functions. The code below works (in a way lol). The problem is when it detects that a username or email exists, it returns "Email is already in use." or "Username is already in use." and then still executes the last promise p3 which adds the user to the database (which it shouldn't). Maybe I'm misconceiving the concept. What is the right way to do this?

var p1 = new Promise(
    (res, rej) => {

        Database.doesEmailExist(userObj.email,

            (err, exists) => {

                if(err) return rej(err);
                if(exists) return rej("Email is already in use.");
                return res();
            }

        );


    }
);

var p2 = new Promise(
    (res, rej) => {

        Database.doesUsernameExist(userObj.username,

            (err, exists) => {

                if(err) return rej(err);
                if(exists) return rej("Username is already in use.");
                return res();

            }

        )
    }
);

var p3 = new Promise(
    (res, rej) => {

        Database.addUserToDB(userObj.username, userObj.email, bcrypt.hashSync(userObj.password),

            (err) => {

                if(err) return rej(err);
                return res();

            }

        )

    }
);

Promise.all([p1, p2, p3]).then(success => {

    return callback("true", "You have successfully registered.");

}).catch(reason => {

    return callback("false", reason);

});

You are running p1, p2 and p3 at the same time: the promises are run, and than Promise.all waits untill all of them are finished.

You should first check if the username and the email are valid, then save the new user.

var checkEmail = () => new Promise(
    (res, rej) => {
        Database.doesEmailExist(userObj.email,

            (err, exists) => {

                if(err) return rej(err);
                if(exists) return rej("Email is already in use.");
                return res();
            }

        );


    }
);

var checkUsername = () => new Promise(
    (res, rej) => {

        Database.doesUsernameExist(userObj.username,

            (err, exists) => {

                if(err) return rej(err);
                if(exists) return rej("Username is already in use.");
                return res();

            }

        )
    }
);

var saveUser = () => new Promise(
    (res, rej) => {

        Database.addUserToDB(userObj.username, userObj.email, bcrypt.hashSync(userObj.password),

            (err) => {

                if(err) return rej(err);
                return res();

            }

        )

    }
);

Promise.all([ checkUsername(), checkEmail() ]).then(saveUser).then(() => {

    return callback("true", "You have successfully registered.");

}).catch(reason => {

    return callback("false", reason);

});

You code can be represented like this: (time is on the x axis)

p1          --------> .
p2          ---->     .
p3          ------>   .
____________________________________________
                      OK ? callback("true")
                      ERROR ? callback("false")

While mine is more like this:

checkEmail()    --------> .
checkUsername() ---->     .
saveUser()                . ---------------> .
________________________________________________
                          OK ? continue      OK ? callback("true")
                          ERROR ?            ERROR ? callback("false")
                                 \__________________/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM