From 7edb1abfb4a4b33cbeec5def924aa7d26c613ad9 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Tue, 12 Nov 2019 16:42:36 +0200 Subject: [PATCH] promise middleware: fix to correctly return an error promise on error. --- src/store/promise-middleware.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/store/promise-middleware.ts b/src/store/promise-middleware.ts index 4f9bf7d..3ca47b5 100644 --- a/src/store/promise-middleware.ts +++ b/src/store/promise-middleware.ts @@ -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 { 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); }