Reputation: 3243
I have a modal popup, which I am coding such that when an open()
function is called, it returns an observable. Any subscriber will then get an object with a property indicating what button was pressed in the modal or whether it was closed, etc.
If a 'success' button is pressed, I want to make a http call, which in turn also returns an observable! How would I combine these two observables? In angular 1 with promises I can return promises from promises, so I would do something like
var promise = modal.open()
.then(function(res) {
if (res.success) {
return httpService.get(); // also returns a promise
}
return res;
});
how would I do something like this for observables?
Upvotes: 1
Views: 1942
Reputation: 202256
You can leverage observable operators to build an asynchronous data flow. In you case, the switchMap
operator:
var observable = modalObservable.switchMap(() => {
return return httpService.get(); // also returns an observable
});
Be careful to import the operators you need since they aren't by default (see this question: Angular 2 HTTP GET with TypeScript error http.get(...).map is not a function in [null]):
import 'rxjs/add/operator/switchMap';
You could have a look at the following article and presentation for more details:
Upvotes: 1