Angular 6: Passing Observable response to another Observable
You can use an operator such as switchMap which per documentation "Maps to observable, complete previous inner observable, emit values". Using switchMap
it will switch to the 2nd HttpClient
call mapping the first observable, when the source of the first HttpClient
calls emits. If a new initial/outer request is executed, it will cancel the in-flight/in-process request.
Keep in mind, the way your alert()
statements are set up, they will simply not work as alert()
will execute before the requests have completed, hence the undefined
. You'd need to execute alert()
or similar within subscribe
or utilize operators such as do
/tap
to ensure the data has actually been returned.
import { switchMap } from 'rxjs/operators';
_httpClient.get('http://localhost:5000/api/mycontroller')
.pipe(
switchMap(output => _httpClient.get('http://localhost:5000/api/mycontroller2' + output))
)
.subscribe(output2 => alert(output2));
If you need to save the outputs to some local class property, you can utilize do/tap to perform an action before/after data is returned/mapped/processed.
import { switchMap, tap } from 'rxjs/operators';
_httpClient.get('http://localhost:5000/api/mycontroller')
.pipe(
tap(output => {
console.log(output);
this.output = output;
}),
switchMap(output => _httpClient.get('http://localhost:5000/api/mycontroller2' + output)),
tap(output2 => {
console.log(output2);
this.output2 = output2;
})
)
.subscribe(output2 => alert(output2));
For completeness of the accepted answer, other alternatives to switchMap
should be mentioned as well.
In contrast to switchMap
, the mergeMap
(also called flatMap
) operator will NOT be cancelled if the "outer observable" emits a second time before the "inner observable" completes. This may not be important in the case of HttpClient.get
, because it typically only emits once, thus never triggering the inner observable again.
However, be aware of the difference between switchMap
and mergeMap
when continuing working with RxJS operators, especially when dealing with a sequential stream of data.