When dealing with observables in a component that makes multiple API calls using a service, is there a way to ensure that one observable completes before another starts?
For example, how can we make sure that the getOptions function finishes executing before the getReport function is triggered? The getReport function depends on data obtained by getOptions.
ngOnChanges() {
if (this.treeResult != null) {
for (var index = 0; index < this.treeResult.length; index++) {
var element = this.treeResult[index];
this.thumbnailView = null;
this.getOptions(element.id);
this.getReport(element.id, this.a, this.b, element.c);
}
}
}
getOptions(id: number) {
this._Service.GetOptions(id)
.subscribe(options => { this.select = options[0].Options1, this.num = options[0].Options1[0].optionId,
error => this.errorMessage = <any>error)
}
getReport(id: number, a: number, b: number, c: string) {
this._Service.GetReport(id, a, b, c)
.subscribe(data => { this.data = data }, error => this.errorMessage = <any>error);
}
If you need more information or clarification, feel free to ask!