Gathering detailed insights and metrics for redux-promise-middleware-actions
Gathering detailed insights and metrics for redux-promise-middleware-actions
Gathering detailed insights and metrics for redux-promise-middleware-actions
Gathering detailed insights and metrics for redux-promise-middleware-actions
@cromwellian/redux-promise-middleware-actions
Redux action creator for making async actions with redux-promise-middleware
redux-logic
Redux middleware for organizing all your business logic. Intercept actions and perform async processing.
redux-pender
redux middleware that helps to manages async actions based on promise
redux-promise-async-actions
redux-promise-async-actions
Redux action creator for making async actions compatible with redux-promise-middleware
npm install redux-promise-middleware-actions
Typescript
Module System
Node Version
NPM Version
77.9
Supply Chain
100
Quality
75.7
Maintenance
100
Vulnerability
100
License
TypeScript (98.8%)
JavaScript (1.2%)
Total Downloads
431,740
Last Day
134
Last Week
1,706
Last Month
7,022
Last Year
74,166
MIT License
32 Stars
47 Commits
3 Forks
1 Watchers
36 Branches
2 Contributors
Updated on May 22, 2025
Minified
Minified + Gzipped
Latest Version
3.1.0
Package Id
redux-promise-middleware-actions@3.1.0
Unpacked Size
43.30 kB
Size
11.06 kB
File Count
25
NPM Version
6.14.4
Node Version
12.16.3
Cumulative downloads
Total Downloads
Last Day
252.6%
134
Compared to previous day
Last Week
-14.9%
1,706
Compared to previous week
Last Month
6.1%
7,022
Compared to previous month
Last Year
-32%
74,166
Compared to previous year
1
Create Redux actions with a type
and payload
in a standardized way. Inspired by redux-actions but simpler and with special support for asynchronous actions (promises).
Has no dependencies and is tiny (~680 bytes gzipped). First class TypeScript support.
Works with redux-promise-middleware to handle asynchronous actions by dispatching pending
, fulfilled
and rejected
events based on the state of the input promise.
Goals of this library:
Note: If you are using TypeScript this library requires TypeScript 3. For TypeScript 2 use version 1 of this library.
You need to install this library as well as redux-promise-middleware.
npm install redux-promise-middleware-actions redux-promise-middleware
Include redux-promise-middleware when you create your store:
1import promiseMiddleware from 'redux-promise-middleware'; 2 3composeStoreWithMiddleware = applyMiddleware( 4 promiseMiddleware, 5)(createStore);
NOTE: This library is not yet compatible with the promiseTypeSuffixes
option of redux-promise-middleware
Synchronous actions works exactly like redux-actions. You supply a function that returns whatever payload the action should have (if any).
1import { createAction } from 'redux-promise-middleware-actions'; 2 3export const foo = createAction('FOO', (num) => num); 4 5dispatch(foo(5)); // { type: 'FOO', payload: 5 }
When handling the action in a reducer, you simply cast the action function to a string to return the type. This ensures type safety (no spelling errors) and you can use code navigation to find all uses of an action.
1const fooType = foo.toString(); // 'FOO'
When you create an asynchronous action you need to return a promise payload. If your action is called FOO
the following events will be dispatched:
FOO_PENDING
is dispatched immediatelyFOO_FULFILLED
is dispatched when the promise is resolved
FOO_REJECTED
is dispatched instead if the promise is rejected1import { createAsyncAction } from 'redux-promise-middleware-actions'; 2 3export const fetchData = createAsyncAction('FETCH_DATA', async () => { 4 const res = await fetch(...); 5 return res.json(); 6}); 7 8dispatch(fetchData()); // { type: 'FETCH_DATA_PENDING' }
An async action function has three properties to access the possible outcome actions: pending
, fulfilled
and rejected
. You can dispatch them directly (in tests etc.):
1dispatch(fetchData.pending()); // { type: 'FETCH_DATA_PENDING' } 2dispacth(fetchData.fulfilled(payload)); // { type: 'FETCH_DATA_FULFILLED', payload: ... } 3dispacth(fetchData.rejected(err)); // { type: 'FETCH_DATA_REJECTED', payload: err, error: true }
But normally you only need them when you are writing reducers:
1case fetchData.pending.toString(): // 'FETCH_DATA_PENDING' 2case fetchData.fulfilled.toString(): // 'FETCH_DATA_FULFILLED' 3case fetchData.rejected.toString(): // 'FETCH_DATA_REJECTED'
Note that if you try and use the base function in a reducer, an error will be thrown to ensure you are not listening for an action that will never happen:
1case fetchData.toString(): // throws an error
To create a type safe reducer, createReducer
takes a list of handlers that accept one or more actions and returns the new state. You can use it with both synchronous and asynchronous action creators.
createReducer(defaultState, handlerMapsCreator)
1import { createAsyncAction, createReducer } from 'redux-promise-middleware-actions'; 2 3const fetchData = createAsyncAction('GET', () => fetch(...)); 4 5const defaultState = {}; 6 7const reducer = createReducer(defaultState, (handleAction) => [ 8 handleAction(fetchData.pending, (state) => ({ ...state, pending: true })), 9 handleAction(fetchData.fulfilled, (state, { payload }) => ({ ...state, pending: false, data: payload })), 10 handleAction(fetchData.rejected, (state, { payload }) => ({ ...state, pending: false, error: payload })), 11]); 12 13reducer(undefined, fetchData()); // { pending: true, data: ..., error: ... }
asyncReducer(asyncActionCreator)
It can get tedious writing the same reducer for every single async action so we've included a simple reducer that does the same as the example above:
1import { asyncReducer } from 'redux-promise-middleware-actions'; 2 3const fetchData = createAsyncAction('GET', () => fetch(...)); 4const fetchReducer = asyncReducer(fetchData); 5 6fetchReducer() // { data?: Payload, error?: Error, pending?: boolean }
You can also combine it with an existing reducer:
1export default (state, action) => { 2 const newState = fetchReducer(state, action); 3 4 switch (action.type) { 5 case 'SOME_OTHER_ACTION': 6 return { ... }; 7 default: 8 return newState; 9 } 10};
You can add metadata to any action by supplying an additional metadata creator function. The metadata creator will receive the same arguments as the payload creator:
createAction(type, payloadCreator, metadataCreator)
1const foo = createAction( 2 'FOO', 3 (num) => num, 4 (num) => num + num 5); 6 7dispatch(foo(5)); // { type: 'FOO', meta: 10, payload: 5 }
createAsyncAction(type, payloadCreator, metadataCreator)
1const fetchData = createAsyncAction( 2 'FETCH_DATA', 3 (n: number) => fetch(...), 4 (n: number) => ({ n }) 5); 6 7dispatch(fetchData(42)); 8// { type: 'FETCH_DATA_PENDING', meta: { n: 42 } } 9// { type: 'FETCH_DATA_FULFILLED', meta: { n: 42 }, payload: Promise<...> } 10// { type: 'FETCH_DATA_REJECTED', meta: { n: 42 }, payload: Error(...) }
You can specify a different type delimiter for your async actions:
createAsyncAction(type, payloadCreator, metadataCreator, options)
1const foo = createAsyncAction( 2 'FETCH_DATA', 3 (n: number) => fetch(num), 4 undefined, 5 { promiseTypeDelimiter: '/' } 6); 7 8dispatch(foo()); 9// { type: 'FETCH_DATA/PENDING' } 10// { type: 'FETCH_DATA/FULFILLED', payload: ... } 11// { type: 'FETCH_DATA/REJECTED', payload: ... }
Thanks to Deox for a lot of inspiration for the TypeScript types.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 1/26 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
11 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-06-09
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More