【发布时间】:2016-04-14 07:05:57
【问题描述】:
为了在我的数据库中创建对象,我已经创建了许多这样的 Promise。
var createUserPromise = new Promise(
function(resolve, reject) {
User.create({
email: 'toto@toto.com'
}, function() {
console.log("User populated"); // callback called when user is created
resolve();
});
}
);
最后,我想按照我想要的顺序调用我的所有承诺。 (因为有些对象依赖于其他对象,所以我需要保持这个顺序)
createUserPromise
.then(createCommentPromise
.then(createGamePromise
.then(createRoomPromise)));
所以我希望看到:
User populated
Comment populated
Game populated
Room populated
不幸的是,这条消息被打乱了,我不明白是什么。
谢谢
【问题讨论】:
-
注意 - 猫鼬已经返回承诺 - 你的代码应该有
new Promise正好零次。请参阅 stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it 和 mongoosejs.com/docs/promises.html
标签: javascript node.js mongoose promise