gpt4 book ai didi

javascript - Bluebird Promise.map 无法正常工作

转载 作者:行者123 更新时间:2023-11-28 05:44:07 26 4
gpt4 key购买 nike

我正在尝试在问题和答案之间建立关联。我使用 Bluebird 的 API .map 来确保重定向仅在所有 question.addAnswers(answer) promise 完成后发生。因此,在我的终端中,我应该看到如下内容:

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

关于javascript - Bluebird Promise.map 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38682490/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com