I'm trying to write a synchronous function call in Angular using a Promise, but it still gets a different result than expected. I have 2 functions that use the service that download data from the API:
getData1(shortcut: string): void {
this.apiServ.getPrice(shortcut).subscribe(
(price: CurrentPrice) => {
this.price = price;
console.log(1);
},
error => this.error = error
);
}
getData2(shortcut: string): void {
this.apiServ.getPrice(shortcut).subscribe(
(price: CurrentPrice) => {
this.price = price;
console.log(2);
},
error => this.error = error
);
}
I'm trying to make the next function call after the response is completed. So:
data(shortcuts) {
this.getData1(shortcuts[0]);
this.getData2(shortcuts[1]);
}
ngOnInit() {
const myPromise = new Promise((resolve) => {
this.data(this.shortcuts);
resolve(3)
});
myPromise.then((value) => {console.log(value)});
}
But my browser prints this call order:
3
1
2
What am I doing wrong?
I also tried to use callbacks but the effect is always the same.