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

1 Answer 1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.