蓝鸟 Promise.map 不工作

Bluebird Promise.map is not working

我正在尝试在问题和答案之间建立关联。我正在使用 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,所以它会 return 一个 promise。结果保持不变。 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"
      });
    };
  });
}

你最突出的问题是 createAndAddToQuestion 没有 return 承诺,所以 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")
  });
}