Last active
February 14, 2019 15:04
-
-
Save PhiLhoSoft/3d4b162b8094a95479fc1b67dc41059f to your computer and use it in GitHub Desktop.
RxJS error management exploration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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