0.0.11 • Published 9 years ago

redux-future v0.0.11

Weekly downloads
4
License
MIT
Repository
github
Last release
9 years ago

redux-future

build status npm version

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 to true.

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

0.0.11

9 years ago

0.0.10

9 years ago

0.0.9

9 years ago

0.0.8

10 years ago

0.0.7

10 years ago

0.0.6

10 years ago

0.0.5

10 years ago

0.0.4

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago