Bluebird Promise.map不起作用

时间:2016-07-31 09:15:53

标签: javascript dictionary promise sequelize.js bluebird

我正在尝试在问题和答案之间建立联系。我正在使用Bluebird的API .map来确保重定向仅在所有question.addAnswers(answer)承诺完成后才会发生。因此,在我的终端,我应该看到这样的事情:

done adding a answer to the question
done adding a answer to the question
finished

但是,我看到的是:

finished
done adding a answer to the question
done adding a answer to the question

因此,我认为Promise.map根本不起作用。我错过了什么?我怎样才能使它发挥作用?

这是我的代码:

router.post('/create', function(req, res) {
  models.Question.create({
    content: req.body.question
  })
  .then(function(question) {
    if (!question) {
      res.render('questions/new', {
          error: "Question \"#{req.body.question}\" fails to be created"
        });
    } else {
      // Update the new question to each user
      models.User.findAll()
      .then(function(users) {
        users.forEach(function(user) {
          user.addQuestion(question)
        });
      });
      Promise.map(req.body.answers, function(answer){
        return createAndAddToQuestion(question, answer, res)
      })
      .then(function(){
        console.log('finished')
        res.redirect("/questions/success/?question_id=" + question.id);
      });
    };
  })
})

var createAndAddToQuestion = function(question, answer, res) {
  models.Answer.create({
    content: answer
  })
  .then(function(ans) {
    if (ans) {
      var promise = question.addAnswer(ans)
      promise.then(function(){
        console.log("done adding a answer to the question")
      });
      return question.addAnswer(ans);
    } else {
      res.render('questions/new', {
        error: "Answer \"#{answer}\" fails to be created"
      });
    };
  });
}

更新 我只是更新createAndAddToQuestion,所以它会返回一个承诺。结果保持不变。 Promise.map无效。

var createAndAddToQuestion = function(question, answer, res) {
  models.Answer.create({
    content: answer
  })
  .then(function(ans) {
    if (ans) {
      return question.addAnswer(ans).then(function() {
        console.log('done')
      })
    } else {
      res.render('questions/new', {
        error: "Answer \"#{answer}\" fails to be created"
      });
    };
  });
}

1 个答案:

答案 0 :(得分:0)

您最突出的问题是createAndAddToQuestion未返回承诺,因此map无法知道要等待的内容。

此外,您不等待models.User.findAll,两次调用question.addAnswer(ans);,如果无法创建答案,并且没有通用的错误处理程序,可能会尝试多次呈现错误消息。你应该做的

router.post('/create', function(req, res) {
  createQuestion(req.body).then(function(question) {
    console.log('finished')
    res.redirect("/questions/success/?question_id=" + question.id);
  }, function(err) {
    res.render('questions/new', {
      error: err.message
    });
  }).catch(function(err) {
    console.error(err);
    res.status(500);
  });
});

function createQuestion(opts) {  
  return models.Question.create({
    content: opts.question
  })
  .then(function(question) {
    if (!question) {
      throw new Error("Question \"#{opts.question}\" fails to be created");
    }
    // Update the new question to each user
    return Promise.all([
      models.User.findAll()
      .then(function(users) {
        users.forEach(function(user) {
          user.addQuestion(question)
        })
      }),
      Promise.map(opts.answers, function(answer){
        return createAndAddToQuestion(question, answer)
      })
    ]).return(question);
  });
}

function createAndAddToQuestion(question, answer) {
  return models.Answer.create({
    content: answer
  })
  .then(function(ans) {
    if (!ans) {
      throw new Error("Answer \"#{answer}\" fails to be created");
    }
    return question.addAnswer(ans);
  })
  .then(function(){
    console.log("done adding a answer to the question")
  });
}