I've got an API call that I need to make multiple times as the API doesn't support requests for multiple items. What I have written so far ends up in an endless loop of sending GET requests to the server. I believe I don't understand the nature of Observables in the below example. My assumption is that each call in the while loop subscribes to a new single object and that when it is resolved it will be placed in the array. How can I modify the below code to achieve what I want?
getSingle(): Observable<Single> {
return this.http.get(this.url, this.options)
.map((r: Response) => r.json().data as Single);
}
getMultiple(num: number): Single[] {
let multiple: Single[] = [];
let i = 0;
while (i < num) {
this.getSingle().subscribe(single => {
multiple.push(single);
console.log('Success');
i++;
}, err => {
console.log('Failure');
});
}
return multiple;
}