然后承诺不起作用

时间:2015-07-22 16:20:22

标签: javascript express promise

我有一个关于db的统计报告的代码。

exports.calculate = function(req, res, next) {
    models.Quiz.count()
    .then(function(questions) {
        statistics.questions = questions;
        models.Comment.count().then(function(comments) {
            statistics.comments = comments;
            statistics.average_comments = (statistics.comments / statistics.questions).toFixed(2);
            models.Quiz.findAll({
                include:    [{model: models.Comment}]})
            .then(function(quizes) {
                for (index in quizes) {
                    if (quizes[index].Comment.length) {
                        statistics.commented_questions++;
                    } else {statistics.no_commented++;}
                };
            })
        })
    })
    .catch(function(error) {next(error)})
    .finally(function() {next()});      
};

它在SQL语句之前正常工作,但从不进行循环,因此我永远无法获得

  

statistics.commented_questions

  

statistics.no_commented

先谢谢了!

1 个答案:

答案 0 :(得分:1)

当将承诺链接在一起时,他们需要知道前一个承诺何时被拒绝或履行。在当前代码中,初始承诺永远不会返回值/ promise,而是调用异步函数。代码基本上就像JS引擎一样:

exports.calculate = function(req, res, next) {
  models.Quiz.count()
    .then(function(questions) {
      statistics.questions = questions;
      // ASYNC FUNCS THAT ARE NEVER RETURNED
      // ...
      // functions in JS without an explicit return statement return nothing (essentially `undefined`)
    })
    .catch(function(error) {
      next(error)
    })
    .finally(function() {
      next()
    });
};

因此,在引擎等待初始承诺完成/拒绝之后,它会触发另一个异步操作的承诺,该异步操作返回一个promise但不会将其返回到原始的promise链。默认情况下,原始承诺链接收undefined,然后传递给链中的下一个方法。在这种情况下,它将是finally方法。

您可能想知道为什么第二个承诺在没有等待的情况下仍在更新信息。这是一种竞争条件,基本上这种承诺正在赢得胜利。

要正确地将承诺链接在一起,您需要将新承诺返回到旧承诺链,如下所示:

exports.calculate = function(req, res, next) {
  models.Quiz.count().then(function(questions) {
    statistics.questions = questions;

    return models.Comment.count();
  }).then(function(comments) {
    statistics.comments = comments;
    statistics.average_comments = (statistics.comments / statistics.questions).toFixed(2);

    return models.Quiz.findAll({
      include: [{
        model: models.Comment
      }]
    });
  }).then(function(quizes) {
    for (index in quizes) {
      if (quizes[index].Comment.length) {
        statistics.commented_questions++;
      } else {
        statistics.no_commented++;
      }
    }
  }).catch(next).finally(next);
};

如果您使用的是支持本机Promise对象的Node / IO版本,您可以利用这一点来发出并发请求,因为它们都不相互依赖。注意:Promise API没有finally()方法,但我们可以使用then()的第二个参数来传递错误。

exports.calculate = function(req, res, next) {
  Promise.all([
    models.Quiz.count(),
    models.Comment.count(),
    models.Quiz.findAll({
      include: [{
        model: models.Comment
      }]
    })
  ]).then(function(results)
    // `results` is an array of [questions, comments, quizes]
    statistics.questions = results[0];
    statistics.comments = results[1];
    statistics.average_comments = (statistics.comments / statistics.questions).toFixed(2);

    for (index in results[2]) {
      if (results[2][index].Comment.length) {
        statistics.commented_questions++;
      } else {
        statistics.no_commented++;
      }
    }
  }).then(next, next);
};
相关问题