简体   繁体   English

等待所有不同的承诺完成 nodejs(异步等待)

[英]Wait for all different promise to finish 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};
})();

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 调用转换为一起运行,然后返回最终输出。

(async() => {
  const [ profile, token ] = await Promise.all([
    profileHelper.getUserData(username),
    tokenHelper.getUserToken(username)
  ]);

  return { profile, token };
})();

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 等到所有 ES6 承诺完成,甚至被拒绝的承诺

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. Promise.all(iterable) 方法返回一个 Promise,当 iterable 参数中的所有承诺都已解决或当 iterable 参数不包含任何承诺时,该 Promise 会解决。 It rejects with the reason of the first promise that rejects.它以第一个拒绝的承诺的原因拒绝。

(async() => {
  const response = await Promise.all([
    profileHelper.getUserData(username),
    tokenHelper.getUserToken(username)
  ]);

  return {profile: response[0], token: response[1]};
})();

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;
})
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM