运行 承诺后的代码在其他代码位置解析
Run code after promise resolves at other code position
我知道标题有点令人困惑,但我不知道如何更准确地描述我的问题 - 对此感到抱歉。
所以更准确地说,我是 javascript 的新手,我有一个采用 MEAN Stack 架构的项目。在我的代码中,我正在更新我的 "Question" 模型的文档和代码中的某个位置。接下来的代码必须 运行 独立于文档是否已经保存 - 除了方法的最后一行,我通过套接字将响应发送回我的 angular 客户端。此外,如您所见,整个问题阵列逐个问题更新,因此很难用简单的 .then 完成,因为我必须以某种方式检查是否已保存整个问题阵列,然后执行我的最后一个命令。
为了说明问题,这里有一些代码片段。
room.connections.forEach(connection => {
Question.find({room: room.title}).then(questions => {
questions.forEach(question => {
if (question.answers.length !== 2) {
question.answers.push({email: connection.userId, own: "", guess: ""});
question.save(); // This is the promise to check for later on in code
}
})
});
});
因此,在这个代码块之后,其他代码行紧随其后,正如我所说,必须 运行 独立于问题是否已经更新。
但是作为我的方法的最后一行,我有以下语句,它发回一个响应以指示客户端可以加载问答游戏——一旦问题被保存在数据库中,这个命令就应该被执行:
} else{
// GameReady is sent and both users open the Game Page
io.of('/game').in(room.title).emit('GameReady', true); // THIS IS THE COMMAND I AM TALKING ABOUT
ack(false);
}
正如我所说,这是我的第一个 javascript 项目,我知道承诺用于此类内容,但通常我可以使用 questions.save().then(.. .."here the rest of the code") - 但在我的例子中,代码 "in between" 应该已经执行过,它是 forEach 中的一个保存,我不知道如何处理这个。
所以,希望您能理解我的问题,非常感谢您的帮助。
如果我没理解错的话,我想你想要Promise.all()
The Promise.all() method returns a single Promise that fulfills when all of the promises passed as an iterable have been fulfilled or when the iterable contains no promises. It rejects with the reason of the first promise that rejects.
如果 question.save()
returns 一个承诺,并且您想 运行 在所有问题保存完成后编码,那么:
Question.find({ room: room.title }).then(questions => {
Promise.all(
// Use a map here so each question generates a promise.
// Resulting in an array of promises.
questions.map(question => {
if (question.answers.length !== 2) {
question.answers.push({ email: connection.userId, own: "", guess: "" })
return question.save() // return the promise so Promise.all can wait for it.
}
}),
).then(() => {
// All questions have been saved. Do the next thing.
io.of("/game")
.in(room.title)
.emit("GameReady", true)
ack(false)
})
})
我知道标题有点令人困惑,但我不知道如何更准确地描述我的问题 - 对此感到抱歉。
所以更准确地说,我是 javascript 的新手,我有一个采用 MEAN Stack 架构的项目。在我的代码中,我正在更新我的 "Question" 模型的文档和代码中的某个位置。接下来的代码必须 运行 独立于文档是否已经保存 - 除了方法的最后一行,我通过套接字将响应发送回我的 angular 客户端。此外,如您所见,整个问题阵列逐个问题更新,因此很难用简单的 .then 完成,因为我必须以某种方式检查是否已保存整个问题阵列,然后执行我的最后一个命令。
为了说明问题,这里有一些代码片段。
room.connections.forEach(connection => {
Question.find({room: room.title}).then(questions => {
questions.forEach(question => {
if (question.answers.length !== 2) {
question.answers.push({email: connection.userId, own: "", guess: ""});
question.save(); // This is the promise to check for later on in code
}
})
});
});
因此,在这个代码块之后,其他代码行紧随其后,正如我所说,必须 运行 独立于问题是否已经更新。 但是作为我的方法的最后一行,我有以下语句,它发回一个响应以指示客户端可以加载问答游戏——一旦问题被保存在数据库中,这个命令就应该被执行:
} else{
// GameReady is sent and both users open the Game Page
io.of('/game').in(room.title).emit('GameReady', true); // THIS IS THE COMMAND I AM TALKING ABOUT
ack(false);
}
正如我所说,这是我的第一个 javascript 项目,我知道承诺用于此类内容,但通常我可以使用 questions.save().then(.. .."here the rest of the code") - 但在我的例子中,代码 "in between" 应该已经执行过,它是 forEach 中的一个保存,我不知道如何处理这个。
所以,希望您能理解我的问题,非常感谢您的帮助。
如果我没理解错的话,我想你想要Promise.all()
The Promise.all() method returns a single Promise that fulfills when all of the promises passed as an iterable have been fulfilled or when the iterable contains no promises. It rejects with the reason of the first promise that rejects.
如果 question.save()
returns 一个承诺,并且您想 运行 在所有问题保存完成后编码,那么:
Question.find({ room: room.title }).then(questions => {
Promise.all(
// Use a map here so each question generates a promise.
// Resulting in an array of promises.
questions.map(question => {
if (question.answers.length !== 2) {
question.answers.push({ email: connection.userId, own: "", guess: "" })
return question.save() // return the promise so Promise.all can wait for it.
}
}),
).then(() => {
// All questions have been saved. Do the next thing.
io.of("/game")
.in(room.title)
.emit("GameReady", true)
ack(false)
})
})