s-leg3ndz
s-leg3ndz

Reputation: 3518

Redux dispatch with Promise

I would like run multiple dispatch with Promise.

I've 2 action, importSelfInfoFromApi and FlashMessage. I would like this :

store.dispatch(importSelfInfoFromApi()).then(() => store.dispatch(setFlashMessage('Synchronisation finished')));

case 'IMPORT_SELF_INFO_FROM_API':
  return new Promise((resolve, reject) => {
    callApi('/users/me', 'get').then((result) => {
      resolve(store.dispatch(importSelfInfoFromApiSuccess(result))); // Success
    }).catch((error) => {
      reject(store.dispatch(importSelfInfoFromApiError(error.message))); // Error
    });
  });

Do you know if i need to use redux-thrunk ? I can't just return Promise in my action ?

Thank you community !

Upvotes: 0

Views: 1513

Answers (1)

The Reason
The Reason

Reputation: 7973

From redux-thunk docs:

Redux Thunk middleware allows you to write action creators that return a function instead of an action.

So seems it's not what are you looking for. You need another one - redux-promise for handling promises in your actions

Upvotes: 1

Related Questions