Angular 2 call function with subscribe inside a for loop

Viewed 9423

I have a function with subscription to a service inside:

selectCar(carNumber) {
  this.carService.getCarByNumerator(carNumber)
    .subscribe( (car) => {
                  console.log(carNumber);
                  //more stuff here
                },
                (err) => console.log(err)
               );
}

I want to call this function inside a for loop as following:

for(let carNumber of carNumbers) {
    this.selectCar(carNumber);
}

The issue is, sometime it works as I expect, but sometimes the order is not as in the list.

E.g. the list is:

45
67
89

but when I look in the console, I see the following:

67
89
45

How can I force the for loop not to go to next item till the current function call finished?

2 Answers
Related