0.6.0 • Published 7 years ago

redux-promise v0.6.0

Weekly downloads
74,170
License
MIT
Repository
github
Last release
7 years ago

redux-promise

build status codecov npm version npm monthly downloads

FSA-compliant promise middleware for Redux.

npm install --save redux-promise

Usage

import promiseMiddleware from 'redux-promise';

The default export is a middleware function. If it receives a promise, it will dispatch the resolved value of the promise. It will not dispatch anything if the promise rejects.

If it receives an Flux Standard Action whose payload is a promise, it will either

  • dispatch a copy of the action with the resolved value of the promise, and set status to success.
  • dispatch a copy of the action with the rejected value of the promise, and set status to error.

The middleware returns a promise to the caller so that it can wait for the operation to finish before continuing. This is especially useful for server-side rendering. If you find that a promise is not being returned, ensure that all middleware before it in the chain is also returning its next() call to the caller.

Using in combination with redux-actions

Because it supports FSA actions, you can use redux-promise in combination with redux-actions.

Example: Async action creators

This works just like in Flummox:

createAction('FETCH_THING', async id => {
  const result = await somePromise;
  return result.someValue;
});

Unlike Flummox, it will not perform a dispatch at the beginning of the operation, only at the end. We're still looking into the best way to deal with optimistic updates. If you have a suggestion, let me know.

Example: Integrating with a web API module

Say you have an API module that sends requests to a server. This is a common pattern in Flux apps. Assuming your module supports promises, it's really easy to create action creators that wrap around your API:

import { WebAPI } from '../utils/WebAPI';

export const getThing = createAction('GET_THING', WebAPI.getThing);
export const createThing = createAction('POST_THING', WebAPI.createThing);
export const updateThing = createAction('UPDATE_THING', WebAPI.updateThing);
export const deleteThing = createAction('DELETE_THING', WebAPI.deleteThing);

(You'll probably notice how this could be simplified even further using something like lodash's mapValues().)

bee-react@intru/react-componentsmf_nslogin@geodashboard/gd-core__old@geodashboard/gd-gltg__oldautoml-templatesage.br.hypercube.web.componentsjoshua-wepy2graasp-insightsreact-redux-appnewreactreduxyyuap-ref-testsice-module-testdevaptdevapt-core-commonreax-helpersau-app-react-findwidgetreact-admin-cng-drumkymove-beneficiaries-front@infinitebrahmanuniverse/nolb-redux-p@everything-registry/sub-chunk-2640tauitc-toolwhcb-contact-formwonderpool-app-driverwonderpool-app-riderwalletsaver-address-search-v2webpack-react-redux-boilerplatews-address-searchscalable-componentsamtec-device-connectsc-sierrareactatorreactivarms-meteor-reduxredux-anyredux-boot-expressredstatestarter-kit-react-reduxtestwexpthe-storethinwebclientvisual-camvisual-reactvoxeet-react-components-edotovp-uimetatonic-reduxhc-redux-middlewareheroku-createreactapp-webhooksgolb-web-uigome-react-uiliattribute_edit_reduxlive-reduxkjb-projectkv-sitekv-utilskv-baseinfinity-modules-uihapipressjust-spajoshua-shop-wepyjoshua-wepyghn-ssomx-frameworkmanuscriptjsnodewrite-core-adminpao-aop-clientopen-freecell-webottrprotiumpublic-radio-native-androidpivotal-release-boardpolomundial.com.arpresidium-jsnoampec-reactpatternplate-clientreact-bandreact-chunkyreact-chunky-contr3fuseradcom-common-componentsreact-giphy-searchreact-f-your-starterkitreact-redux-antdreact-redux-weather-data-chartsreact-redux-starterkit-webpackreact-redux-templatereact-redux-template-binderreact-redux-template-testreact-start-newreact-gradebookreact-native-template-vindicce-exporedux-lzwredux-cropperreact-templetreact-swear-catcherreact-toolbetredux-promise-arrayredux-webpack-es6-boilerplate
0.6.0

7 years ago

0.5.3

9 years ago

0.5.2

9 years ago

0.5.1

9 years ago

0.6.0-alpha

9 years ago

0.5.0

9 years ago

0.4.0

9 years ago

0.3.0

9 years ago

0.2.0

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago