Bluebird 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
,所以它将返回一个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")
});
}
相关文章:
- electronic BrowserWindow的最小高度和宽度在hide()show()方法之后不起作用
- 监视函数从服务返回不起作用,但作用域函数起作用
- 幻灯片滚动javascript不起作用
- 简单的javascript在Shopify中不起作用
- Recaptcha在IE7和IE8中不起作用
- Ember Data DS.Model's set函数不起作用
- 使用map来检查是否为真'不起作用
- Image Map 在 FF 和 IE 中不起作用(使用 onclick javascript)
- Google Map API v3 在 Wordpress 中不起作用
- AngularJS谷歌地图(ng-map)图标网址不起作用
- _.map() 在播放真实数据时不起作用
- 为什么map.setCenter不起作用
- Google Web Map 复选框在 Firefox 和 Chrome 中有效,但在 IE 中不起作用
- 为什么这个map reduce Promises数组不起作用,而只是减少它起作用
- Ng-map on-click在移动设备上不起作用
- Javascript _map () vs array.map();为什么一个在这里起作用而另一个不起作用
- 为什么这个.map函数不起作用
- Bluebird Promise.map不起作用
- Google Map Javascript API 不起作用
- Javascript“map”在IE中不起作用