javascript 等待所有不同的承诺完成 nodejs(异步等待)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48663080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wait for all different promise to finish nodejs (async await)
提问by undefined
I am currently waiting for all the promise to finish sequentially like this:
我目前正在等待所有的承诺像这样按顺序完成:
(async() => {
let profile = await profileHelper.getUserData(username);
let token = await tokenHelper.getUserToken(username);
console.log(profile);
console.log(token);
return {profile: profile, token: token};
})();
But this way, profile and token executes sequentially. Since both are independent of each other, I want both of them to be executed independently together. I think this can be done using Promise.all, but I am not sure of the syntax and I could not find any help as well.
但是这样,配置文件和令牌按顺序执行。由于两者相互独立,我希望它们一起独立执行。我认为这可以使用 Promise.all 来完成,但我不确定语法,我也找不到任何帮助。
So my question is how I can convert above api calls to run together and then return the final output.
所以我的问题是如何将上述 api 调用转换为一起运行,然后返回最终输出。
回答by Jose Paredes
(async() => {
const [ profile, token ] = await Promise.all([
profileHelper.getUserData(username),
tokenHelper.getUserToken(username)
]);
return { profile, token };
})();
回答by messerbill
use Promise.all()method:
使用Promise.all()方法:
(async() => {
let [ profile, token ] = await Promise.all(
[profileHelper.getUserData(username),
tokenHelper.getUserToken(username)
])
return {profile: profile, token: token};
})();
Wait until all ES6 promises complete, even rejected promises
回答by Michal
You want to use Promise.all
你想使用Promise.all
The Promise.all(iterable) method returns a single Promise that resolves when all of the promises in the iterable argument have resolved or when the iterable argument contains no promises. It rejects with the reason of the first promise that rejects.
Promise.all(iterable) 方法返回一个 Promise,当 iterable 参数中的所有承诺都已解决或当 iterable 参数不包含任何承诺时,该 Promise 会解决。它以第一个拒绝的承诺的原因拒绝。
(async() => {
const response = await Promise.all([
profileHelper.getUserData(username),
tokenHelper.getUserToken(username)
]);
return {profile: response[0], token: response[1]};
})();
回答by Vadivel Subramanian
The Promise.all method returns a single Promise that resolves when all of the promises in the argument have resolved or when the argument contains no promises.
Promise.all 方法返回一个 Promise,当参数中的所有承诺都已解决或参数不包含承诺时,该 Promise 会解决。
exports.getServerDetails = async (req, res, next) => {
var getCount = [];
const [ onlineUser, countSchool ] = await Promise.all([
getOnlineUsers(), // online user count
getRegisterUser(), // register user
getRegisterSchools(), // register school count
]);
getCount = [
{"Online Users": onlineUser},
{"Registered Users" : countSchool}
];
sendJSONresponse(res, 200, {
status: 'success',
data: getCount
})
}
async function getOnlineUsers() {
return Login.count({'onlineStatus': 1}, (err, count) => {
if (err) {
return err;
}
return count;
});
}
async function getRegisterUser() {
return Login.count({}, (err, totResUser) => {
if (err) {
return err;
}
return totResUser;
})
}
async function getRegisterSchools() {
return Login.count({'role': 'Admin'},(err, totalSchool) => {
if (err) {
return err;
}
return totalSchool;
})
}

