Return Observable From Inside Nested Callbacks Functions
Good afternoon! I'm currently developing a web app in Angular2/4 and I have a problem with Observables. The goal is, inside a component, to call a function and when that function f
Solution 1:
What You are looking for is switchMap or mergeMap. switch map is better it will cancel previous request if new comes out.
myFunc1(): Observable<Status> {
// some code and then call of myFunc2()
return this.myService2.myFunc2()
.switchMap(data => {
// do some staff here and then call of myFunc3()
return this.myService3.myFunc3()
}).switchMap(data2 => {
// do some other staff and then call of myFunc4()
return this.myService4.myFunc4()
}).switchMap(data3 => {
// do staff and then return program flow into the Component
return Observable.of<Status>({status: 'ok'});
});
});
});
}
Post a Comment for "Return Observable From Inside Nested Callbacks Functions"