0

My function return value of an array before pushing value into array.

private async getPPickerid(ppvalue,AssignedToIdArr):Promise<any> {
return new Promise<any>((resolve, reject) => {
  ppvalue.forEach(async(element ,i) => {
    var user = await this.GetUserIdPPicker(element.Key);
    console.log(user.d.Id);
     AssignedToIdArr.push(user.d.Id); 
  })
resolve(AssignedToIdArr);
}) 

Function doesn't wait for the loop to be completed for pushing the value into array.

Instead It resolved the blank array and then array. Push work.

I have used async-await but not getting any way for function to wait for loop and then get resolved

2

Try using this:

private async function getPPickerid(ppvalue: any[], AssignedToIdArr: any[]): Promise<any[]> {
  for (let index = 0; index < ppvalue.length; index++) {
    let user = await this.GetUserIdPPicker(ppvalue[index].Key);
    console.log(user.d.Id);
    AssignedToIdArr.push(user.d.Id);
  }
  return AssignedToIdArr;
}
1
  • 1
    thanks it worked
    – abhishek23
    Nov 26 at 5:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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