等待所有不同的承诺完成 nodejs(异步等待) [英] Wait for all different promise to finish nodejs (async await)
本文介绍了等待所有不同的承诺完成 nodejs(异步等待)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在等待所有的承诺像这样按顺序完成:
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};
})();
但是这样,配置文件和令牌按顺序执行.由于两者相互独立,我希望它们一起独立执行.我认为这可以使用 Promise.all 来完成,但我不确定语法,也找不到任何帮助.
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.
所以我的问题是如何将上述 api 调用转换为一起运行,然后返回最终输出.
So my question is how I can convert above api calls to run together and then return the final output.
推荐答案
(async() => {
const [ profile, token ] = await Promise.all([
profileHelper.getUserData(username),
tokenHelper.getUserToken(username)
]);
return { profile, token };
})();
这篇关于等待所有不同的承诺完成 nodejs(异步等待)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文