Skip to content

Instantly share code, notes, and snippets.

@bloodyKnuckles
Created August 16, 2017 19:22
Show Gist options
  • Save bloodyKnuckles/0573eba38fb5b2ca1e6fd77622084aec to your computer and use it in GitHub Desktop.
Save bloodyKnuckles/0573eba38fb5b2ca1e6fd77622084aec to your computer and use it in GitHub Desktop.
cycle js HTTP wrapper, error handling
// 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