For Loop not working

#1

how to i do a for loop for this as i have dynamic variable

for(let i = 0; i < task.approver.length;i++){

this.getOfficerNames(task.approver[i]).then((data:any)=>{
    this.conVariable=data["0"].key;

this.getCount(this.conVariable).then((data:any)=>{

this.conCount=data.length;

this.taskDetailsRef.set((this.conCount+1).toString(),task.taskName);

})

  })
  
}

its seems like the app did the for loop first even before entering the method. Any ideas would be appreciated. Thanks
ps. just started programming.

#2
angular.forEach(task.approver, function(item, key){
  this.getOfficerNames(item).then((data:any)=>{
    this.conVariable=data[0].key;
  });
});

If your are using ionic v1 then try to read angularjs documentation for basic javascript programming questions,
there isangular.isUndefined() or angular.copy() read more about it.

I don’t really like how this looks like, try to improve your API response in order to access to a propierty and not to a index position.

Sorry for my bad english.

1 Like
#3

The above function call is a asynchronous. So it will not wait till its execution to continue loop, that is the reason loop is getting finished first before it executes that method.

You can use asyn & await to avoid that behaviour.