在Observables的forkJoin文档中,它说args可以是一个数组,但没有列出一个示例来展示如何使用:
https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/forkjoin.md
我尝试了类似以下代码的函数,但遇到了错误:
:3000/angular2/src/platform/browser/browser_adapter.js:76
EXCEPTION: TypeError: Observable_1.Observable.forkJoin is not a function
以下是我函数的剪切版本:
processStuff( inputObject ) {
let _self = this;
return new Observable(function(observer) {
let observableBatch = [];
inputObject.forEach(function(componentarray, key) {
observableBatch.push(_self.http.get(key + '.json').map((res: Response) => res.json()));
});
Observable.forkJoin(
observableBatch
// );
).subscribe(() => {
observer.next();
observer.complete();
});
});
}
我的问题根源涉及到一个循环,在这里要求循环结束后再继续进行,详情请参见:Angular2 Observable - how to wait for all function calls in a loop to end before proceeding?。但是,我还没有完全掌握如何正确使用带有数组参数的forkJoin以及正确的语法。非常感谢您能提供的帮助。
注:第三个返回Observable的函数示例
thirdFunction() {
let _self = this;
return Observable.create((observer) => {
// return new Observable(function(observer) {
...
observer.next(responseargs);
observer.complete();
});
}
processStuff(inputObject) {
let _self = this;
let observableBatch = [];
inputObject.forEach((componentarray, key) => {
observableBatch.push(_self.thirdFunction().map((res: Response) => res.json()));
});
return Observable.forkJoin(observableBatch);
}
elsewhere() {
this.processStuff(inputObject)
.subscribe()
}
import 'rxjs/add/observable/forkJoin'
或者import 'rxjs/Rx'
? - Sasxa