有条件地在承诺链上提前退出而不会引发错误

用户名

在我的node js应用程序中,我试图创建一个需要执行一些异步步骤的注册路由。在较早的步骤中,有一种情况我想停止并只返回一条消息,但是我对将条件混入承诺中感到困惑。

我的具体问题在以下伪代码中的注释中:

router.post('/register', function(req, res, next) {
    var identity = req.body.identity;
    var password = req.body.password;

    // can't register without a reservation
    hasReservation(identity).then(function(hasReservation) {
        if (hasReservation) { return register(identity, password); }
        else {
            // stuck here: I want to say:
            res.json({ registered: false, reason: "missing reservation" });
            // and then I want to stop further promises
            // but this isn't an error, I don't want to throw an error here
        }
    }).then(function(user) {
        // in my app, registering an existing user is okay
        // I just want to reset a few things about the user and save
        // but I don't want to be here if there's no reservation
        if (user) {
            user.foo = 'bar';
            return user.save().then(function() { return user; });
        } else {
            return register(identity, password);
        }
    }).then(function(user) {
        // I want to do more asynch stuff here
    }).then(function(result) {
        res.json(result);
    }).catch(function(error) {
        res.status(500).json(error);
    });
});

在第一个承诺完成后,如何有条件地“纾困”而不抛出错误?

杰罗曼达X

如果register(identity,password)返回一个promise,则可以按以下方式重新组织代码:

router.post('/register', function(req, res, next) {
    var identity = req.body.identity;
    var password = req.body.password;

    // can't register without a reservation
    hasReservation(identity)
    .then(function(hasReservation) {
        if (hasReservation) { 
            return register(identity, password)
            .then(function(user) {
                // in my app, registering an existing user is okay
                // I just want to reset a few things about the user and save
                // but I don't want to be here if there's no reservation
                if (user) {
                    user.foo = 'bar';
                    return user.save().then(function() { return user; });
                } else {
                    return register(identity, password);
                }
            })
            .then(function(user) {
                // I want to do more asynch stuff here
            })
            .then(function(result) {
                res.json(result);
            })
        } else {
            // stuck here: I want to say:
            res.json({ registered: false, reason: "missing reservation" });
            // and then I want to stop further promises
            // but this isn't an error, I don't want to throw an error here
        }
    })
    .catch(function(error) {
        res.status(500).json(error);
    });
});

否则,如果register(..)仅返回一个值,则将Register(...)的FIRST实例包装在Promise中。

        return Promise.resolve(register(identity, password))

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章