【发布时间】:2020-05-10 22:01:24
【问题描述】:
let data = users.find({})
.toArray()
.then((result) => {
state.response.end(JSON.stringify({
message: result
}));
})
.catch((err) => {
state.response.end(JSON.stringify({
error: err.message,
}));
log.error(failed to process the request: ", err);
});
if(!!data){
return collage.find({_id: params._id}).toArray()
.then((collages: any[]) => {
if (!!collages) {
return //some more code
}
})
.then(() => {
return university.find({_id: params._id}).toArray()
})
.then((user: any[]) => {
if (!!user) {
return //some more code
}
})
.then(() => {
response.writeHead(200, {'Content-Type': 'application/json'});
response.end(JSON.stringify({status: " Request processed."}));
})
.catch((err) => {
response.writeHead(200, {'Content-Type': 'application/json'});
response.end(JSON.stringify({status: "error", + err.message}));
});
}
一次性返回所有的 promise 结果
return {
collage:collages,
university:users
}
在这里,我使用 Promise(无异步和等待)从多个集合中获取数据。我想先从用户集合中检索数据,然后再从其他集合中检索数据。如果条件首先返回而不等待用户收集结果,则内部代码。是这样写的吗?还是?
【问题讨论】:
-
为什么不能使用 async/await?
-
请用未声明的变量是什么来完成您的问题。
标签: node.js typescript promise