Last active
March 22, 2018 13:47
-
-
Save vaidd4/0d4b3f62faa95a1348bb79295d85843b to your computer and use it in GitHub Desktop.
RxJS/Observable logging before `subscribe()`
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
/** | |
* Simple logging on an observable, without altering the subscription. | |
* | |
* @param {Observable<any>} obs - Observable (RxJS) | |
* @param success - success message | |
* @param failure - error message | |
* @returns {Observable<any>} | |
*/ | |
function logObservable (obs, success, failure) { | |
if (success) { | |
obs = obs.map(value => { | |
console.log(success) | |
return value | |
}) | |
} | |
if (failure) { | |
obs = obs.catch(err => { | |
console.error(failure, err) | |
throw err | |
}) | |
} | |
return obs | |
} | |
/** USAGE */ | |
const request = HTTP.get('http://my.domain/path') | |
logObservable(request, 'GET successful', 'GET Error:') | |
request.subscribe(value => console.log('GOT:', value)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment