0

Is there an option to get back response from redux-api-middleware inside this function?

dispatch(someAction()).then(() => {
  // CONSOLE.LOG RESPONSE HERE
});
Adee
  • 357
  • 1
  • 3
  • 13
  • Look into [redux-thunk](https://github.com/gaearon/redux-thunk) for how to dispatch async actions – Aron Mar 06 '18 at 14:48
  • The action is done correctly and I know how to store its result in redux state. The question is how to return the answer to the above function :) – Adee Mar 06 '18 at 14:56

1 Answers1

1

Starting with an example from the redux-api-middleware docs.

const USER_REQUEST = '@@user/USER_REQUEST'
const USER_SUCCESS = '@@user/USER_SUCCESS'
const USER_FAILURE = '@@user/USER_FAILURE'

const getUser = () => ({
  [RSAA]: {
    endpoint: 'https://hostname/api/users/',
    method: 'GET',
    headers: { 'Content-Type': 'application/json' },
    types: [
      USER_REQUEST,
      USER_SUCCESS,
      USER_FAILURE
    ]
  }
})

If you are using redux-thunk you can do the following:

dispatch(getUser())
  .then((payload) => {
    console.log(payload, 'oh hai, payload');
  });
gburnett
  • 755
  • 8
  • 18