Skip to content

Instantly share code, notes, and snippets.

@ssougnez
Created March 13, 2021 23:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssougnez/e7e8049870a2565e290625d9d75f5967 to your computer and use it in GitHub Desktop.
Save ssougnez/e7e8049870a2565e290625d9d75f5967 to your computer and use it in GitHub Desktop.
import { Observable, of } from 'rxjs';
import { catchError, finalize } from 'rxjs/operators';
const observable = new Observable<number>(observer => {
let x = 0;
const interval = setInterval(() => {
if (x === 5) {
observer.error('Oops');
}
else {
observer.next(x++);
}
}, 1000);
return {
unsubscribe: () => clearInterval(interval)
}
});
observable
.pipe(
catchError(err => of(-1)),
finalize(() => console.log('Cleaning up...'))
)
.subscribe({
next: x => console.log(`${x} received`),
complete: () => console.log('Completed'),
error: err => console.warn(err)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment