promise middleware: fix to correctly return an error promise on error.

master
Tom Hacohen 5 years ago
parent c7d4a15f5f
commit 7edb1abfb4

@ -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);
}

Loading…
Cancel
Save