Created
March 13, 2021 23:34
-
-
Save ssougnez/ac3442c65474bdad5916ee28dd736aab to your computer and use it in GitHub Desktop.
This file contains 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
import { Observable, of, throwError } from 'rxjs'; | |
import { catchError, finalize, map } from 'rxjs/operators'; | |
const observable = new Observable<number>(observer => { | |
let x = 0; | |
const interval = setInterval(() => { | |
if (x === 0) { | |
observer.error('Oops'); | |
} | |
else { | |
observer.next(x++); | |
} | |
}, 1000); | |
return { | |
unsubscribe: () => clearInterval(interval) | |
} | |
}); | |
observable | |
.pipe( | |
catchError(() => { | |
console.warn('Looks like 5 has been emitted...') ; | |
return of(0); | |
}), | |
map(x => { | |
const a: string = null; | |
const b = a.substring(1, 2); | |
return parseInt(b); | |
}), | |
catchError(err => { | |
console.warn("OMG, I'm so unlucky"); | |
return throwError(err); | |
}), | |
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