function isPromise ( val ) { return val && typeof val . then === "function" ; } export default function promiseMiddleware ( { dispatch } ) { return ( next ) => ( action ) => { if ( isPromise ( action . payload ) ) { dispatch ( { ... action , payload : undefined } ) ; return action . payload . then ( ( result ) => dispatch ( { ... action , payload : result } ) ) . catch ( ( error ) => { dispatch ( { ... action , payload : error , error : true } ) ; return Promise . reject ( error ) ; } ) ; } else { return next ( action ) ; } } ; }