|
|
|
@ -1,6 +1,6 @@
|
|
|
|
|
// Based on: https://github.com/acdlite/redux-promise/blob/master/src/index.js
|
|
|
|
|
|
|
|
|
|
function isPromise(val: any) {
|
|
|
|
|
function isPromise(val: any): val is Promise<any> {
|
|
|
|
|
return val && typeof val.then === 'function';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -9,10 +9,12 @@ export default function promiseMiddleware({ dispatch }: any) {
|
|
|
|
|
if (isPromise(action.payload)) {
|
|
|
|
|
dispatch({ ...action, payload: undefined });
|
|
|
|
|
|
|
|
|
|
return action.payload.then(
|
|
|
|
|
(result: any) => dispatch({ ...action, payload: result }),
|
|
|
|
|
(error: Error) => dispatch({ ...action, payload: error, error: true })
|
|
|
|
|
);
|
|
|
|
|
return action.payload
|
|
|
|
|
.then((result: any) => dispatch({ ...action, payload: result }))
|
|
|
|
|
.catch((error: Error) => {
|
|
|
|
|
dispatch({ ...action, payload: error, error: true });
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
return next(action);
|
|
|
|
|
}
|
|
|
|
|