https://github.com/zavalit/redux-payload-promise
Async Redux Action on a Promise base
https://github.com/zavalit/redux-payload-promise
Last synced: about 1 year ago
JSON representation
Async Redux Action on a Promise base
- Host: GitHub
- URL: https://github.com/zavalit/redux-payload-promise
- Owner: zavalit
- Created: 2016-06-21T12:18:35.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2016-06-21T14:33:10.000Z (almost 10 years ago)
- Last Synced: 2025-03-12T18:43:12.196Z (about 1 year ago)
- Language: JavaScript
- Size: 1.95 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Redux Async Action Middleware
This redux middleware enables a dispatching of async actions.
## Usage
To use it in your project you have to provide it on your redux middleware stack:
```javascript
import promiseMiddleware from 'redux-payload-promise';
import { createStore, applyMiddleware } from 'redux';
const store = createStore(
reducers,
[ 'some provided state' ],
applyMiddleware(promiseMiddleware)
);
```
and define an Action with object defined with a **payload** key, that refer to your async Promise.
For example:
```javascript
export const loadDataAction = (queryParams) =>
({
type: LOAD_DATA,
payload: loadPromise(queryParams),
});
```
in case of a successful Response it will be dispatched with a JSON object like this:
```javascript
{
payload:"[object Promise]"
result: // your load data query result string
success:true
}
```
in nonsuccessful case the ```success``` will be false.
You can chain as many dispatches as you want:
```javascript
dispatch(loadDataAction(queryParams))
.then((output) => dispatch(filterResonse(JSON.parse(output.result))))
.then(() => dispatch(sayEverythingIsFine()));
```