Skip to content Skip to sidebar Skip to footer

Async Waterfall Equivalent With Q

I've got a single page which is an account settings page. In it, I allow my users to update their avatar (if they've attached an image), change their email (if it has been changed

Solution 1:

If you are using Q.defer you are generally doing something wrong.

var findByEmail = Q.nbind(User.findByEmail, User);
var updateEmail = Q.nbind(User.updateEmail, User);
var updateName = Q.nbind(User.updateName, User);

//later on...

exports.settingsAccountPOST = function (req, res) {
    findByEmail({
        email: req.body.email
    })
    .then(function (user) {
        if (!user) {
            return updateEmail({
                userId: req.session.user.id,
                email: req.body.email
            });
        }
    })
    .then(function () {
        return updateName({
            userId: req.session.user.id,
            name: req.body.name
        })
    })
    .then(function () {
        res.redirect("/account");
    })
    .catch(function(e){
        //Handle any error
    });
};

Post a Comment for "Async Waterfall Equivalent With Q"