redux-promise-middleware
Advanced tools
Comparing version 2.2.2 to 2.2.3
@@ -51,6 +51,9 @@ 'use strict'; | ||
var isActionOrThunk = function isActionOrThunk(resolved) { | ||
return typeof resolved === 'function' || resolved.meta || resolved.payload; | ||
var isAction = function isAction(resolved) { | ||
return resolved.meta || resolved.payload; | ||
}; | ||
var isThunk = function isThunk(resolved) { | ||
return typeof resolved === 'function'; | ||
}; | ||
/** | ||
@@ -62,11 +65,12 @@ * Return either the fulfilled action object or the rejected | ||
var resolved = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
return dispatch(_extends({ | ||
return isThunk(resolved) ? dispatch(resolved) : dispatch(_extends({ | ||
type: type + '_' + FULFILLED | ||
}, isActionOrThunk(resolved) ? resolved : _extends({}, resolved && { payload: resolved }, meta && { meta: meta }))); | ||
}, function (error) { | ||
return dispatch(_extends({ | ||
type: type + '_' + REJECTED, | ||
payload: error, | ||
}, isAction(resolved) ? resolved : _extends({}, resolved && { payload: resolved }, meta && { meta: meta }))); | ||
}, function () { | ||
var resolved = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
return isThunk(resolved) ? dispatch(resolved) : dispatch(_extends({ | ||
type: type + '_' + REJECTED | ||
}, isAction(resolved) ? resolved : _extends({ | ||
error: true | ||
}, meta && { meta: meta })); | ||
}, resolved && { payload: resolved }, meta && { meta: meta }))); | ||
}); | ||
@@ -73,0 +77,0 @@ }; |
{ | ||
"name": "redux-promise-middleware", | ||
"version": "2.2.2", | ||
"version": "2.2.3", | ||
"description": "Redux middleware for handling promises", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -24,3 +24,3 @@ # Redux Promise Middleware | ||
The pending action is dispatched immediately with the original type string and a suffix of `_PENDING`. The fulfilled action is dispatched only if the promise is resolved, e.g., if it was successful; and the rejected action is dispatched only if the promise is rejected, e.g., if an error occurred. The fulfilled and rejected suffixes are `_FULFILLED` and `_REJECTED` respectively. | ||
The pending action is dispatched immediately with the original type string and a suffix of `_PENDING`. The fulfilled action is dispatched only if the promise is resolved, e.g., if it was successful; and the rejected action is dispatched only if the promise is rejected, e.g., if an error occurred. The fulfilled and rejected suffixes are `_FULFILLED` and `_REJECTED` respectively. If necessary, it is possible [to change the value](#type-suffix-configuration) of the type suffixes. | ||
@@ -49,7 +49,39 @@ ```js | ||
## Type Suffix Configuration | ||
## Dispatching actions when promises are resolved | ||
When adding the middleware to your middleware composition layer, you can supply an optional options object. This object accepts an array of suffix strings that can be used instead of the default `['PENDING', 'FULFILLED', 'REJECTED']` with a key of `promiseTypeSuffixes`. | ||
Often times when a promise is resolved, one might want to fire a one or more "callback" actions to respond to the resolved promise. One example is changing the route after a user is successfully signed in. | ||
If you need to do this, you can dispatch a second action: | ||
```js | ||
const actionCreator = () => ({ | ||
type: 'FIRST_ACTION_TYPE', | ||
payload: { | ||
promise: Promise.resolve({ | ||
type: 'SECEOND_ACTION_TYPE' | ||
payload: ... | ||
}) | ||
} | ||
}); | ||
``` | ||
It is also possible to use a function: | ||
```js | ||
const actionCreator = () => ({ | ||
type: 'FIRST_ACTION_TYPE', | ||
payload: { | ||
promise: Promise.resolve((dispatch, getState) => { | ||
dispatch({ type: 'SECEOND_ACTION_TYPE', payload: ... }) | ||
dispatch(someActionCreator()) | ||
}) | ||
} | ||
}); | ||
``` | ||
## Type suffix configuration | ||
When adding the promise middleware to your middleware stack, you can supply an optional configuration object. This object accepts an array of suffix strings that can be used instead of the default `['PENDING', 'FULFILLED', 'REJECTED']` with a key of `promiseTypeSuffixes`. | ||
```js | ||
applyMiddleware( | ||
@@ -56,0 +88,0 @@ promiseMiddleware({ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
24121
516
0
111