Created
June 27, 2017 21:31
-
-
Save BerkeleyTrue/8b7636230254d0dc30b4b3242a568cd7 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
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 | |
}); | |
} |
Both accomplish the same thing. You second example is much more declarative, though
Gotcha. Thanks, this is great
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ah, nice. I like this.
In this case, is there any difference between
defer
and chainingof/map/mergeMap
?and