For the following TypeScript (using rxjs):
getRegularData(): Observable<MyData> {
return WS.loadRegularData();
}
getAlternateData(): Observable<MyData> {
return WS.loadAlternateData();
}
how can a new method be implemented to satisfy the following pseudocode:
getData(): Observable<MyData> {
// try to use getRegularData, and return observable for result.
// if getRegularData returns null, get data from getAlternateData()
// instead and return observable for result.
}
There are many ways you can implement this, one would be to use a switchMap that contains your condition:
getData(): Observable<MyData> {
return getRegularData()
.switchMap(data => {
if (data != null) {
return Observable.of(data);
} else {
return getAlternateData();
}
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With