admin管理员组文章数量:1314832
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
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
Share Improve this question edited Mar 31, 2017 at 10:16 martin 97k26 gold badges203 silver badges235 bronze badges asked Mar 31, 2017 at 5:57 user2869612user2869612 6192 gold badges13 silver badges34 bronze badges 2-
I don't understand how
forkJoin
wasn't useful in this situation.forkJoin
will for all your observables to finish. Then you can run your db query. Isn't it what you want? – eko Commented Mar 31, 2017 at 6:00 - determineFate() uses some 'this' variables which are getting overwritten by parallel threads. I have already tried forkJoin() but due to above issue, need to execute all in series. – user2869612 Commented Mar 31, 2017 at 6:10
1 Answer
Reset to default 9Using Observable.concat(ObservableArray)
will just flatten the array and emit each Observable from ObservableArray
one by one. Btw, using the static version of concat
makes sense only with two or more parameters (see http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#static-method-concat).
Instead you can iterate the array of Observables and wait until they plete one by one with the concatAll()
operator.
This example simulates your use-case:
var observableArray = [];
// filling up Observable array dynamically
for (var i = 0; i < 10; i++) {
observableArray.push(Observable.of('Value ' + i));
}
Observable.from(observableArray)
.concatAll()
.subscribe(console.log, null, () => console.log('pleted'));
The Observable.from()
emits each Observable separately and concatAll()
subscribes to each one of them in the order they were emitted.
This demo prints to console the following output:
Value 0
Value 1
Value 2
Value 3
Value 4
Value 5
Value 6
Value 7
Value 8
Value 9
pleted
本文标签: javascriptExecute dynamically created array of observables in seriesStack Overflow
版权声明:本文标题:javascript - Execute dynamically created array of observables in series - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741964485a2407468.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论