Skip to content

Instantly share code, notes, and snippets.

@PhiLhoSoft

PhiLhoSoft/index.ts

Last active Feb 14, 2019
Embed
What would you like to do?
RxJS error management exploration
// index.ts as typed in https://stackblitz.com/edit/rxjs-iqjrzu
import { of, throwError } from 'rxjs';
import { map, switchMap, catchError, tap, finalize } from 'rxjs/operators';
const source = of('World').pipe(
map(x => { return { good: `Hello ${x}!` }; }),
// Comment out for testing regular behavior
switchMap((x) => throwError('bad')),
);
source.pipe(
// Comment out to get subscribe's error handling
//*
catchError((e) => {
// Doesn't break the stream (tap is called), return the given value to onNext, also call onCompleted.
return [{ error: e }];
// Calls onCompleted, no onNext call.
//return [];
// Rethrow an error, calls onError.
//return throwError('Not recoverable');
}),
//*/
tap(() => {
console.info('Just tapping a bit');
}),
finalize(() => {
console.info('Final step, always done');
}),
).subscribe(
x => console.log('Result:', x),
e => console.error('Error:', e),
() => console.info('Complete'),
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.