等待所有不同的承诺完成nodejs(async await) [英] Wait for all different promise to finish nodejs (async await)

查看:219
本文介绍了等待所有不同的承诺完成nodejs(async await)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在等待所有承诺按顺序完成:

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(async await)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆