如何在拒绝承诺时停止执行?

时间:2017-03-01 22:00:52

标签: javascript node.js

我试图从promises而不是所有这些嵌套函数中受益。下面的代码工作(以某种方式lol)。问题是,当它检测到存在用户名或电子邮件时,它会返回"Email is already in use.""Username is already in use.",然后仍然会执行最后一个承诺p3,它将用户添加到数据库中(它不应该' T)。也许我误解了这个概念。这样做的正确方法是什么?

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);

});

1 个答案:

答案 0 :(得分:1)

你正在同时运行p1,p2和p3:承诺运行,而Promise.all等待,直到所有这些都完成。

您应首先检查用户名和电子邮件是否有效,然后保存新用户。

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);

});

您的代码可以这样表示:(时间在x轴上)

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

虽然我更像是这样:

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