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 // 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'; return val && typeof val.then === 'function';
} }
@ -9,10 +9,12 @@ export default function promiseMiddleware({ dispatch }: any) {
if (isPromise(action.payload)) { if (isPromise(action.payload)) {
dispatch({ ...action, payload: undefined }); dispatch({ ...action, payload: undefined });
return action.payload.then( return action.payload
(result: any) => dispatch({ ...action, payload: result }), .then((result: any) => dispatch({ ...action, payload: result }))
(error: Error) => dispatch({ ...action, payload: error, error: true }) .catch((error: Error) => {
); dispatch({ ...action, payload: error, error: true });
return Promise.reject(error);
});
} else { } else {
return next(action); return next(action);
} }

Loading…
Cancel
Save