# redux-simplepromise

> FSA-compliant promise middleware for redux with optimistic update support.

Latest version **1.2.0** (published 2016-02-26) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install redux-simplepromise
pnpm add redux-simplepromise
yarn add redux-simplepromise
bun add redux-simplepromise
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2016-02-26 |
| First published | 2016-02-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Bob Monteverde |
| Maintainers | bobmonteverde |
| Keywords | redux, promise, middleware, redux-middleware, flux |

## Links

- npm: https://www.npmjs.com/package/redux-simplepromise
- Repository: https://github.com/bobmonteverde/redux-simplepromise
- Issues: https://github.com/bobmonteverde/redux-simplepromise/issues
- npm.io page: https://npm.io/package/redux-simplepromise

## Alternatives

- [@reckona/mreact-store](https://npm.io/package/@reckona/mreact-store.md) — 976 weekly downloads
- [regular-state](https://npm.io/package/regular-state.md) — 410 weekly downloads
- [@pacote/flux-actions](https://npm.io/package/@pacote/flux-actions.md) — 65 weekly downloads
- [@pilotlab/lux-debug](https://npm.io/package/@pilotlab/lux-debug.md) — 39 weekly downloads
- [vue-persist-state](https://npm.io/package/vue-persist-state.md) — 19 weekly downloads

## Recent versions

- 1.2.0 (latest) — 2016-02-26
- 1.1.0 — 2016-02-26
- 1.0.3 — 2016-02-26
- 1.0.2 — 2016-02-26
- 1.0.1 — 2016-02-26
- 1.0.0 — 2016-02-26

## README

# redux-simplepromise
[FSA](https://github.com/acdlite/flux-standard-action)-compliant promise middleware for redux with optimistic update support, making async data loading in Redux as simple as any other action.

## Install

```js
npm install --save redux-simplepromise
```

## Usage

First, import the middleware, and pass it as an argument to applyMiddleware.

```js
import { createStore, applyMiddleware } from 'redux'
import promiseMiddleware from 'redux-simplepromise'
import rootReducer from './reducers'

const store = createStore(
  rootReducer,
  applyMiddleware(promiseMiddleware)
)
```

Now you can call an action with a Promise as it's payload.

This can be used with JavaScript's new 'fetch' function that returns a promise, for example:

```js
export const getData = () => ({
  type: 'GET_DATA',
  payload: fetch(data_API_URL, { method: 'GET' })
})
```

Redux actions with a promise as their payload are split into separate actions that can be handled synchronously.

Immediately after calling **getData**, an action with the type **GET_DATA_REQUEST** is dispatched, before the promise is resolved (this action has no payload, but anything passed in the original action's **meta** key will be passed with the action. This allows for optimistic updates, or to set an **isLoading** value in the state.
If the promise is successfully resolved, the action **GET_DATA** is dispatched with the resolved value as it's payload.
If the Promise is rejected, the action **GET_DATA_FAIL** is dispatched with the **Error object** provided in action's **error** key.

The reducer for the **GET_DATA** action could look something like this:

```js
import { request, reject, resolve } from 'redux-simplepromise'

function users(state = {}, action) {
  switch (action.type) {
    case resolve('GET_DATA'): // defaults to 'GET_DATA'
      return Object.assign({}, state, {
        data: action.payload,
        isFetchingData: false
      })
    case request('GET_DATA'):
      return Object.assign({}, state, {
        isFetchingData: true
      })
    case reject('GET_DATA'):
      return Object.assign({}, state, {
        dataError: action.error,
        isFetchingData: false
      })
    default:
      return state
  }
}
```

You can also pass the Promise with other values in the payload, and the other values will be passed to the **request** action as **action.payload**, and the **resolve and reject** actions as **action.meta.originalPayload**:

```js
import { request, reject, resolve } from 'redux-simplepromise'

export const getData = () => ({
  type: 'GET_DATA',
  payload: {
    promise: fetch(data_API_URL, { method: 'GET' }),
    dataType: 'users'
  }
})

function users(state = {}, action) {
  switch (action.type) {
    case resolve('GET_DATA'):
      return Object.assign({}, state, {
        data: action.payload,
        isFetchingData: false,
        type: action.meta.originalPayload.dataType
      })
    case request('GET_DATA'):
      return Object.assign({}, state, {
        isFetchingData: true,
        type: action.payload.dataType
      })
    case reject('GET_DATA'):
      return Object.assign({}, state, {
        dataError: action.error,
        isFetchingData: false,
        type: action.meta.originalPayload.dataType
      })
    default:
      return state
  }
}
```

The helper functions **request** , **reject**, and **resolve** simply add the appropriate suffix to the action's type.  By default:

```js
request('GET_DATA') === 'GET_DATA_REQUEST'
reject('GET_DATA') === 'GET_DATA_FAIL'
resolve('GET_DATA') === 'GET_DATA'
```

### Configuration

You can customize the **request, reject, and resolve suffix's** by importing the middleware creator **simplePromiseMiddleware** instead of the **promiseMiddleware** directly. For example:

```js
import { simplePromiseMiddleware } from 'redux-simplepromise'

let promiseMiddleware = simplePromiseMiddleware('_START', '_REJECT', '_RESOLVE')
```

Now the **request** and **reject** functions will produce:

```js
request('GET_DATA') === 'GET_DATA_START'
reject('GET_DATA') === 'GET_DATA_REJECT'
resolve('GET_DATA') === 'GET_DATA_RESOLVE'
```

All actions meet the [FSA](https://github.com/acdlite/flux-standard-action) standards.

---
_Source: https://npm.io/package/redux-simplepromise · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
