I am new to Angular2 and observables. I am trying to return the result of a chain of observables to my resolver and couldn't find an answer on SO. Everything works fine when I return only one observable, but fails when those are chained. I've simplified the code to calling consecutively twice the same test function :
@Injectable()
export class SwResolve implements Resolve<any> {
constructor(private swService: SwService) {}
resolve (route: ActivatedRouteSnapshot): Observable<MyObject> | Promise<any> | any {
this.swService.getTestData().subscribe(data => {
return this.swService.getTestData();
});
}
}
SwComponent :
export class SwComponent implements OnInit {constructor(
private route: ActivatedRoute,
private router: Router
) { }
ngOnInit(): void {
this.route.data.forEach((data: any) => {
// Handle data received
});
}
};
SwService :
getTestData() {
return Observable.create(observer => {
let testObject : SwDataObject = {
'labels':['value0','value1'],
'desktop':[123,456],
'mobile':[789,1011]
};
observer.next(testObject);
observer.complete();
});
}
What am I doing wrong here ?
Thanks !
Edit Jun 17: Adding some more info, as it took me a while to wrap my head around RxJs' methods (flatMap, mergeMap, ...). The main reason was because I just didn't get the functional aspect of them. I encourage people in the same boat to have a look at these excellent series of articles on functional programming by Charles Scalfani.