You don't need to subscribe in example1(), example2(), example3().
Return in async callback method subscribe is not return of function.
example1(): Observable<any> | any {
console.log('1');
return this.example2().pipe(
catchError(err => {
return throwError(err);
})
);
}
example2(): Observable<any> | any {
console.log('2');
return this.example3().pipe(
catchError(err => {
return throwError(err);
})
);
}
example3() {
console.log('3');
return of('return from example 3');
}
StackBlitz
UPDATE (15.03.2019)
With your comment, the code should be:
export class AppComponent {
response;
private init$: Observable<any>;
public ngOnInit() {
//get Data from remote
this.example3().subscribe(value => {
//Do primary somethimg with result
let calcVal = value *2;
// Do remote something with data
this.example2(calcVal).subscribe(newValue => {
// Result with updated data
console.log(newValue);
this.response = newValue;
});
});
}
// Async do something with data
example2(value: number): Observable<string>{
return of('Value is: ' + value);
}
// Async get data
example3(): Observable<number> {
console.log('3');
return of(3);
}
}
StackBlitz
Or with switchMap:
export class AppComponent {
response;
private init$: Observable<any>;
public ngOnInit() {
let obs = this.example3().pipe(switchMap(val => this.example2(val)));
obs.subscribe(result => this.response = result);
}
// Async do something with data
example2(value: number): Observable<string>{
return of('Value is: ' + value);
}
// Async get data
example3(): Observable<number> {
console.log('3');
return of(3);
}
StackBlitz