12

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.

So my question is how I can convert above api calls to run together and then return the final output.

2
  • 3
    let [profile, token] = await Promise.all([profileHelper.getUserData(username), tokenHelper.getUserToken(username)])
    – Keith
    Commented Feb 7, 2018 at 11:51
  • stackoverflow.com/a/37576787/1641941
    – HMR
    Commented Feb 7, 2018 at 11:53

4 Answers 4

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

  return { profile, token };
})();
1
  • 2
    Both the answers were almost at same time with almost same explanation.. so choosing randomly.
    – undefined
    Commented Feb 7, 2018 at 12:00
15

use Promise.all() method:

(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

1
  • 1
    Both the answers were almost at same time with almost same explanation.. so choosing randomly.
    – undefined
    Commented Feb 7, 2018 at 12:00
10

You want to use 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.

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

  return {profile: response[0], token: response[1]};
})();
0
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.

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.