27 lines
633 B
TypeScript
27 lines
633 B
TypeScript
|
|
||
|
import { Observable } from 'rxjs/Observable';
|
||
|
import 'rxjs/add/observable/of';
|
||
|
|
||
|
// #docregion
|
||
|
|
||
|
import { ajax } from 'rxjs/observable/dom/ajax';
|
||
|
import { map, catchError } from 'rxjs/operators';
|
||
|
// Return "response" from the API. If an error happens,
|
||
|
// return an empty array.
|
||
|
const apiData = ajax('/api/data').pipe(
|
||
|
map(res => {
|
||
|
if (!res.response) {
|
||
|
throw new Error('Value expected!');
|
||
|
}
|
||
|
return res.response;
|
||
|
}),
|
||
|
catchError(err => Observable.of([]))
|
||
|
);
|
||
|
|
||
|
apiData.subscribe({
|
||
|
next(x) { console.log('data: ', x); },
|
||
|
error(err) { console.log('errors already caught... will not run'); }
|
||
|
});
|
||
|
|
||
|
// #enddocregion
|