When i click on button i am making multiply http request for same api service. If i want to wait for them i am using async await with promises.
NOTE THAT THIS METHOD CALL RETURNS OBSERVABLE !
this.getAuctionService.getAllAuctionsUsingPOST
onButtonClick() {
makeRequest();
makeRequest();
makeRequest();
makeRequest();
}
makeRequest() {
try {
let response: any = this.getAuctionService.getAllAuctionsUsingPOST(params).toPromise()
this.filterService.auctionTotalPages = response.totalPages;
this.currentPage = response.currentPage;
this.remapLazyLoadedData(response, cb);
} catch (error) {
this.toastr.error(error.error.description, error.error.error);
}
}
so when i click on the button four http requests are happening in parallel and that means that sometimes the response from httpRequest number 3 can come earlier then the first one, even it was called later.
Because of this problem i am using now async await with promises
onButtonClick() {
await makeRequest();
await makeRequest();
await makeRequest();
await makeRequest();
}
async makeRequest() {
try {
let response: any = await this.getAuctionService.getAllAuctionsUsingPOST(params).toPromise()
...
} catch (error) {
this.toastr.error(error.error.description, error.error.error);
}
}
now i am awaiting each call and the responses of the calls wwill be always in the same order as they were called.
But i can't find solutuion for this with Observable - rxjs way
How can i await this multiply calls without async and await ?
onButtonClick() {
makeRequest();
makeRequest();
makeRequest();
makeRequest();
}
makeRequest() {
let response: any = this.getAuctionService.getAllAuctionsUsingPOST(params);
....
}
**what i tried**
i tried with `.subscribe` but it does not work - every each call is not waiting for the previous one