I am working on a project (Angular2) where I am creating Observables dynamically and putting them in an array
var ObservableArray : Observable<any>[] = [];
//filling up Observable array dynamically
for (var i = 0; i < this.mainPerson.children.length; i++) {
ObservableArray.push(Observable.fromPromise(this.determineFate(this.mainPerson.children[i])));
}
}
var finalObservable: Observable<any> = Observable.concat(ObservableArray);
finalObservable
.subscribe( data => {
//here I expected to execute determineFate() for all observables inside array
console.log("determine fate resolved data returned [" + data + "]");
}, error => {
console.error("error on Age Year for Characters")
},() => {
//Here I expect this gets executed only when all Observables inside my array finishes
console.log("determine fate resolved data returned COMPLETED");
//DB call
});
determineFate(..): Promise<boolean> {
...
return either true / false if success or error;
}
I want to execute all observables in a series (forkJoin seems to run in parallel - so used concat). Once all observables are executed, want to execute some DB related code. But it seems my code inside 'Completed' block does not wait for all Observables to finish. How can I achieve this?
Thanks in advance
forkJoinwasn't useful in this situation.forkJoinwill for all your observables to finish. Then you can run your db query. Isn't it what you want?