End Software Engineer @ HelloSign • Author of “React Under the Hood” and the upcoming book “Hacking the Front End Interview” • Contributor to popular open source projects such as ReactJS and Redux • Google me Advanced Async in Redux ForwardJS
we’re still trying to figure out the best way to do async. In Redux, thunks, promises, observables, generators, and other patterns are our options. Advanced Async in Redux ForwardJS
front end • In fact, this is a really good thing • It’s slowing down and will continue to slow down. This is also a good thing The JavaScript Cambrian Explosion Advanced Async in Redux ForwardJS
so great for other things. • Separate concerns — React should just be your view layer • Core business logic should be kept outside of UI components React Won in the UI but It's Not a Full Architecture Advanced Async in Redux ForwardJS
uni-directional data flow similar to React • Removes business logic out of your React views • Very small (only 300 lines of code) Redux: Pros Advanced Async in Redux ForwardJS
Doesn’t have an out of the box async story • You need to build an architecture on top of Redux to handle async • Good news is, Redux has a way to be extended via middleware Redux: Cons Advanced Async in Redux ForwardJS
the moment it reaches the reducer. • Uses currying to allow you to create custom dispatch functions (middleware) that are called sequentially in between dispatching and the reducer • Essentially, you can hook into the dispatcher with a chain of functions Redux Middleware Advanced Async in Redux ForwardJS
and build on top of that with middleware. • We can use the Redux store to track the state of async operations, but middleware manages operation of async operations. Why Middleware? Advanced Async in Redux ForwardJS
is a function that wraps an expression to delay its evaluation. • Normal actions are objects. redux-thunk allows you to dispatch a function which will be called by redux-thunk middleware. • redux-thunk will pass in dispatcher and getState functions so you can dispatch an action within a thunk Redux Thunk Advanced Async in Redux ForwardJS
async logic across multiple parts of the application • Not FSA compliant • No way to cancel a dispatched thunk • Not very good at handling streams of data from sources such as a web socket Redux Thunk: Cons Advanced Async in Redux ForwardJS
for async, it’s a more logical choice • Use pburtchaell/redux-promise-middleware. It’s the best one out there with good conventions. Promise Middleware Advanced Async in Redux ForwardJS
(FRP) is your thing, this is the library for you. • It’s a robust abstraction, but it doesn’t feel like it belongs in JavaScript. • It’s a DSL is massive and confusing. • Many incredibly intelligent people are fans of RxJS. • Kind of clunky to use with Redux. RxJS Advanced Async in Redux ForwardJS
on generators. • Used at HelloSign in production for nearly a year. • Instead of dispatching Thunks, you create Sagas to gather all your Side Effects logic in a central place. • This means application logic lives in 2 places • Reducers are responsible for handling state transitions between actions. • Sagas are responsible for orchestrating complex/asynchronous operations. Redux Saga Advanced Async in Redux ForwardJS
multiple workflows, each providing compensating actions for every step of the workflow where it can fail. • You can think of Sagas as daemons, a long living process for orchestrating transactions and handling recovery from failures. The Saga Pattern Advanced Async in Redux ForwardJS
• Turns your async code almost into a reducer. • You can find all your async code in one place. • Explicit handling of failure states. • Generators are part of the language, unlike observables. • Can write async code that looks synchronous • No dependency injection or mocking/stubbing needed for testing. Redux Saga: Pros Advanced Async in Redux ForwardJS
The internals are stable but a little complicated. • If you’re targeting older browsers you’ll need a generator runtime Redux Saga: Cons Advanced Async in Redux ForwardJS