Awesome
redux-future
FSA-compliant future monad middleware for Redux.
This is based on redux-promise.
npm install --save redux-future
Usage
import futureMiddleware from 'redux-future';
The default export is a middleware function. If it receives a future, it will dispatch the resolved value of the future (after forking the future). It will dispatch the error if one occures.
If it receives an Flux Standard Action whose payload
is a future, it will fork
and then either
- dispatch a copy of the action with the resolved value of the future.
- dispatch a copy of the action with the rejected value of the future, and set
error
totrue
.
Example
const result = new Future((reject, resolve) =>
resolve([1, 2, 3, 4, 5, 6]));
const resultFiltered = result.map(
R.compose(
R.assoc('numbers', R.__, { type: 'FILTER_NUMBERS' })
, R.filter(R.gt(3))
)); // will hold [1, 2]
store.dispatch(resultFiltered);
Using in combination with redux-actions
Because it supports FSA actions, you can use redux-future in combination with redux-actions.
Example: Action creators
const result = new Future((reject, resolve) =>
resolve([1, 2, 3, 4, 5, 6])
);
const resultFiltered = result.map(R.filter(R.gt(3))); // will hold [1, 2]
createAction('FILTER_ASYNC', () => resultFiltered);
// or
const filterAction = createAction('FILTER_ASYNC');
filterAction(resultFiltered);
Example: Future(IO)
You can use redux-future
together with redux-io
.
// futureIo :: Future(IO(String))
const futureIo = new Future((rej, res) => {
const io = IO(() => location.href);
setTimeout(() => res(io), 2000);
});
const action = createAction('FSA_ACTION');
store.dispatch(action(futureIo));
Related
Resources
Don't know what a future is? Read the following blog post or watch the video.
Libraries
- folktale data.task
- ramda-fantasy
- fantasy-future
- futurize - Turn callback-style functions or promises into futures
- redux-io - FSA-compliant IO monad middleware for redux
- redux-either - FSA-compliant Either monad middleware for redux