异步瀑布相当于带Q [英] Async waterfall equivalent with Q

查看:220
本文介绍了异步瀑布相当于带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 from the original), and change their name and password.

现在,我使用的是异步的瀑布方法,但我换出对于q异步,因为我preFER的语法(和API)。我不知道是否这是我应该取代异步的瀑布可以用品质的方式。

Right now, I'm using async's waterfall method, but am swapping out async for Q since I prefer the syntax (and api). I'm wondering if this is the way that I should be using Q in replacement of async's waterfall.

我在做这样的事情:

exports.settingsAccountPOST = function(req, res) {
  var doesEmailExist = function() {
    var deferred = Q.defer();

    User.findByEmail({
      email: req.body.email
    }, function(err, user) {
      if (err) {
        deferred.reject(err);
      } else {
        deferred.resolve(user);
      }
    });
    return deferred.promise;
  };

  var updateEmail = function(email) {
    var deferred = Q.defer();

    User.updateEmail({
      userId : req.session.user.id,
      email : req.body.email
    }, function(err, updated) {
      if (err) {
        deferred.reject(err);
      } else {
        deferred.resolve(updated);
      }
    });
    return deferred.promise;
  };

  var updateName = function() {
    var deferred = Q.defer();

    if (req.body.name) {
      User.updateName({
        userId: req.session.user.id,
        name: req.body.name
      }, function(err, updated) {
        if (err) {
          deferred.reject(err);
        } else {
          deferred.resolve(updated);
        }
      });
      return deferred.promise;
    }
  };

  doesEmailExist().then(function(email) {
    if (!email) {
      return(updateEmail(email));
    }
  }).then(function() {
    return(updateName())
  }).then(function() {
    res.redirect('/account')
  });
};

说,有与电子邮件地址错误被使用。有没有办法把它传递到最后的电话吗?用例:更新后的密码正确,但电子邮件更新没有工作,所以我想展示一个会话闪到用户告诉他们正确地更新自己的密码,但与更新他们的电子邮件问题

Say that there is an error with the email address being used. Is there a way to "pass" it to the final call? Use case: Updated password properly, but email update didn't work, so I want to show a session flash to the user telling them they updated their password properly, but there was an issue with updating their email.

我一直在寻找的文档,似乎我可能需要使用:

I was looking in the docs and it seems I may need to use:

.fin(function () {
});

这是正确的?如果是这样,我应该是传递到了吗?只是推到一个对象,内链发生了错误,然后遍历所有错误,并将其展示给用户?或者只是立即返回并显示错误?

Is this correct? If so, what should I be passing into that? Just push to an object the error that occurred within the chain and then loop through all errors and display them to the user? Or just return immediately and display the error?

推荐答案

如果您使用的是 Q.defer 你一般都做错了。

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

这篇关于异步瀑布相当于带Q的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆