actionsPromise.js 537 B

123456789101112131415
  1. const actionPending = name => ({type:'PROMISE',name, status: 'PENDING'})
  2. const actionFulfilled = (name,payload) => ({type:'PROMISE',name, status: 'FULFILLED', payload})
  3. const actionRejected = (name,error) => ({type:'PROMISE',name, status: 'REJECTED', error})
  4. export const actionPromise = (name, promise) =>
  5. async (dispatch) => {
  6. dispatch(actionPending(name))
  7. try {
  8. let payload = await promise;
  9. dispatch(actionFulfilled(name, payload));
  10. return payload
  11. }
  12. catch(error){
  13. dispatch(actionRejected(name, error))
  14. }
  15. }