# redux-with-promise

> A middleware for redux to handle promised based async actions.

Latest version **1.0.0** (published 2018-10-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install redux-with-promise
pnpm add redux-with-promise
yarn add redux-with-promise
bun add redux-with-promise
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2018-10-10 |
| First published | 2018-10-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 12.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Yash Soni |
| Maintainers | yashsoni |
| Keywords | redux, async, promise, middleware, thunk |

## Links

- npm: https://www.npmjs.com/package/redux-with-promise
- Repository: https://github.com/yashatgit/redux-with-promise
- Homepage: https://github.com/yashatgit/redux-with-promise#readme
- Issues: https://github.com/yashatgit/redux-with-promise/issues
- npm.io page: https://npm.io/package/redux-with-promise

## Alternatives

- [promise-throttle](https://npm.io/package/promise-throttle.md) — 44.2K weekly downloads
- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2018-10-10

## README

# redux-with-promise

A middleware for redux to handle promised based async actions.

## Installation and Setup
`npm install redux-with-promise`

Add `withPromise` in your middleware stack.

```
import { applyMiddleware, createStore } from 'redux';
import thunk from 'redux-thunk';
import withPromise from 'redux-with-promise';

const store = createStore(
  yourApp,
  applyMiddleware(thunk, withPromise)
)
```


## Would you need it?

`redux-with-promise` was built with an attempt to reduce the boilerplate code that goes into handling Async actions.

Let's suppose you need to load some users from an external API.
To handle the users-loading, users-error and users-loaded stages in the UI, we would need a sample code look like this:

```
const loadUsers = () => (dispatch, getState) => {
  dispatch({ type: 'USERS_FETCH' });

  someApi.get('/users')
    .then((users) => {
      dispatch({
        type: 'USERS_LOADED',
        payload: users,
      });
    })
    .catch((error) => {
      dispatch({
        type: 'USERS_FETCH_ERROR',
        error,
      });
    });
};

const userReducer = (state, action) => {
  switch (action.type) {
    case 'USERS_FETCH':
      return {
        ...state,
        busy: true,
      };
    case 'USERS_LOADED':
      return {
        ...state,
        busy: false,
        users: action.payload
      };
    case 'USERS_FETCH_ERROR':
      return {
        ...state,
        busy: false,
        error: action.error,
      };
  }
};
```


This would be a general behaviour for any Promise based handling. Though there is no issues with the above code,
`redux-with-promise` helps to reduce this boilerplate code something like this:

```
const loadUsersWithPromise = () => (dispatch, getState) => {
  dispatch({ type: 'USERS_FETCH' });

  dispatch({
    type: 'USERS_FETCH',
    successType: 'USERS_LOADED',
    rejectType: 'USERS_FETCH_ERROR',
    promise: Ajax.get('/users')
  });
};

const userReducerWithPromise = (state, action) => {
  switch (action.type) {
    case 'USERS_FETCH':
    case 'USERS_LOADED':
    case 'USERS_FETCH_ERROR':
      return {
        ...state,
        busy: action.busy,
        users: action.payload,
        error: action.error,
      };
  }
};
```

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