0

I've a _each() that inside got an await where I check a user state in MySQL

the mysql response return after cycle is already completed. there is a way that wait the method checkUserState(), beforse and than go to next element of _each()?

 let obj = _.each(guest_list.guests, async function (value){
     let user_state = await DataController.checkUserState('2305');
    value.Owner.state = user_state;
    console.log('state:' + user_state);
    return value;
  });

  console.log('cycle complete')
 return obj;

Result is: cycle complete state:notdisturb state:notdisturb state:notdisturb state:notdisturb state:notdisturb state:notdisturb state:notdisturb state:notdisturb state:notdisturb state:notdisturb state:notdisturb state:notdisturb

Mariano
  • 473
  • 3
  • 12

1 Answers1

0

You need to use Promise.all

let obj = await Promise.all(_.map(guest_list.guests, async function (value){
     let user_state = await DataController.checkUserState('2305');
    value.Owner.state = user_state;
    console.log('state:' + user_state);
    return value;
  }));

  console.log('cycle complete')
 return obj;
abhishek sahu
  • 648
  • 4
  • 8