Making an Asynchronous call inside a for loop in Angular 2
------------------------------------------------------------------------

One of the frustrating problems of Angular 2, is that when you call an asynchronous method inside a for loop. It happens that, before the call returns for 1st iteration, the second iteration is called and this messes up the value of variables.

Solution: One should use the flatMap here, to make sure that call from 2nd iteration does not happen unless call returns and completes for the 1st iteration.

Observable.of(localListID).flatMap((ellistId) => ellistId).subscribe((pData) => {
//Make asynchronous call here, using pData.
}

Here localListID is array, and we want to use elements of this array to make
asynchronous calls.

Comments

Popular posts from this blog