承诺 - 抓内线

时间:2016-08-29 16:02:31

标签: node.js promise

我有以下代码,带有一些链式承诺。我试图在内部then中抛出一个我希望被外部catch捕获的错误,但它不是当前的行为:

User.getById(userId).then((user: any) => {
    if (user.email != email) {
        User.getByEmail(email).then((user: any) => {
                throw new OperationError("exists"));
        }).catch(StorageError, (err: any) => {
            user.email = email;
            return user.save();
        });
    } else {
        return user.save();
    }
}).then((user: any) => {
    return {
        ok: true,
        user: user
    };
}).catch(OperationError, (error: any) => {
    return {
        ok: false,
        message: error.message
    };
}).asCallback(reply);

如何使throw语句成为外部捕获目标?

修改 更新代码为Vohuman建议。

User.getById(userId).then((user: any) => {
    finalUser = user;

    if (user.email != email) {
        return User.getByEmail(email);
    }
    else {
    //I would like for this response [1] to be used in [2]
        return Promise.resolve(user);
    }
}).then((user: any) => {
    throw new OperationError("Email already exists");
}).catch(StorageError, (error: any) => {

    finalUser.email = email;
    return finalUser.save();
}).then((user: any) => {
    //[2] here is where I would like the else statement to come 

    sendEmail(user.email, subject, content);
    return { ok: true, user: user };
}).catch(OperationError, (error: any) => {

    return { ok: false, message: error.message };
}).asCallback(reply);

我现在如何在第一个else语句中解析用户而不将其捕获到以下then中?

如果电子邮件不存在于数据库中,或者电子邮件与请求帐户相同(重新发送电子邮件确认),则可以发送电子邮件确认。如果电子邮件已经存在,我想中止执行。

1 个答案:

答案 0 :(得分:2)

你的方法完全没问题,你只忘了一点return。没有它,内心的承诺就会被等待,而拒绝不会触发外部的catch

User.getById(userId).then((user: any) => {
    if (user.email != email) {
        return User.getByEmail(email).then((user: any) => {
//      ^^^^^^
            throw new OperationError("exists"));
        }).catch(StorageError, (err: any) => {
            user.email = email;
            return user.save();
        });
    } else {
        return user.save();
    }
}).then((user: any) => {
    return {
        ok: true,
        user: user
    };
}).catch(OperationError, (error: any) => {
    return {
        ok: false,
        message: error.message
    };
}).asCallback(reply);