Created
August 16, 2017 19:22
-
-
Save bloodyKnuckles/0573eba38fb5b2ca1e6fd77622084aec to your computer and use it in GitHub Desktop.
cycle js HTTP wrapper, error handling
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
// Jan van Brügge @jvanbruegge | |
function wrapHTTP(http$) { | |
return http$ | |
.map(res => res.replaceError(err => ({ type: 'left', value: err } as Either<Response, HTTPError>))) | |
.flatten() | |
.map(res => res.type === undefined ? { type: 'right', value: res } : res ); | |
} | |
// mz3 @mz3 | |
function wrapHTTP_Alt(http$) { | |
const response$ = http$ | |
.map(res => res.replaceError(err => xs.of({err}))) | |
.flatten() | |
.map(res => res.hasOwnProperty('err') ? res : {res}); | |
const success$ = response$.filter(obj => obj.hasOwnProperty('res')).map(obj => obj.res); | |
const error$ = response$.filter(obj => obj.hasOwnProperty('err')).map(obj => obj.err); | |
return { | |
success$, | |
error$ | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment