Bluebird Promise.map不起作用

Bluebird Promise.map is not working

本文关键字:不起作用 map Promise Bluebird      更新时间:2023-09-26

我正在尝试在问题和答案之间建立关联。我正在使用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。结果保持不变。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不返回promise,因此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")
  });
}