Created
February 19, 2017 17:50
-
-
Save dkarchmer/4135ec58e99a0ae9bc41a7763123efd0 to your computer and use it in GitHub Desktop.
Example of Angular2/TypeScript function returning an Observable after executing two nested forkJoin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public fetchABunchOfData (rootObj: RootObj): Observable<Project> { | |
// Returned original RootObj as an Observer when nested resource is fetched | |
let returnedData = new ReplaySubject(1); | |
let firstObservable = Observable.forkJoin( | |
this._cloud.getSomeRestData(rootObj.id), | |
this._cloud.getOtherRestData(rootObj.id) | |
); | |
// Execute first two Rest calls (someData and OtherData) | |
firstObservable.subscribe( | |
data => { | |
let someData: Array<SomeData> = data[0]; | |
let otherData: Array<OtherData> = data[1]; | |
let someDataObservers = []; | |
let someDataDict: SomeDataDictionary = {}; | |
someData.forEach(obj => { | |
// console.log(obj); | |
someDataObservers.push(this._cloud.getSomeDataNestedResource(obj.id)); | |
}); | |
// Fetch all nested resources | |
Observable.forkJoin(someDataObservers).subscribe( | |
(nestedData) => { | |
returnedData.next(rootObj); | |
} | |
); | |
}, | |
err => { | |
console.error(err); | |
} | |
); | |
return returnedData; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment