Skip to content

Instantly share code, notes, and snippets.

@BerkeleyTrue
Created June 27, 2017 21:31
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 BerkeleyTrue/8b7636230254d0dc30b4b3242a568cd7 to your computer and use it in GitHub Desktop.
Save BerkeleyTrue/8b7636230254d0dc30b4b3242a568cd7 to your computer and use it in GitHub Desktop.
function epic(actions){
return actions.ofType('FOO')
.mergeMap(action => {
const transformed = transform(action); // may throw collapsing the pipe
return makeThingDoAsyncy(transform)
.catch(e => [foo(e)]); // won't catch above
});
}
// imperative
function epic(actions){
return actions.ofType('FOO')
.mergeMap(action => {
try {
const transformed = transform(action);
} catch(e) {
return Observable.of(foo(e)); // have to handle errors twice
}
return makeThingDoAsyncy(transform)
.catch(e => [foo(e)]); // won't catch above
});
}
// declarative
function epic(actions){
return actions.ofType('FOO')
.mergeMap(action => {
return Observable.defer(() => {
const transformed = transform(action); //errors are thrown in subpipe
return makeThingDoAsyncy(transform);
})
.catch(e => [foo(e)]); // catches all errors
});
}
@iansinnott
Copy link

Ah, nice. I like this.

In this case, is there any difference between defer and chaining of/map/mergeMap?

      return Observable.defer(() => {
        const transformed = transform(action); //errors are thrown in subpipe
        return makeThingDoAsyncy(transform);
      })
        .catch(e => [foo(e)]); // catches all errors

and

      return Observable.of(action)
        .map(transform)
        .mergeMap(makeThingDoAsyncy)
        .catch(e => [foo(e)]); // catches all errors

@BerkeleyTrue
Copy link
Author

Both accomplish the same thing. You second example is much more declarative, though

@iansinnott
Copy link

Gotcha. Thanks, this is great

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment