How can I make the following code wait for forkJoin execution so I can return results from bulkRequest?
constructor(private http: Http) {
let results = this.bulkRequest(this.get("https://jsonplaceholder.typicode.com/posts/1"),
this.get("https://jsonplaceholder.typicode.com/posts/1"),
this.get("https://jsonplaceholder.typicode.com/posts/1"));
console.log("results");
}
get(url: string): Observable<any> {
return this.http.get(url).map(response => response.json());
}
bulkRequest(...sources: SubscribableOrPromise<any>[]): any[] {
let results: any[];
Observable.forkJoin(
sources
).subscribe(response => {
console.log("responses");
results = response;
}, error => {
console.log("error: " + error.toString());
},
() => {
console.log("finally");
});
return results;
}
Currently I'm getting the following console output and results variable is undefined:
results
responses
finally
The observable do execute, if I put a breakpoint at console.log("results") line I see all three of them as Pending in Network tab.