const actionPending  = name => ({type: 'PROMISE', status: 'PENDING', name})
const actionResolved = (name, payload) => ({type: 'PROMISE', status: 'RESOLVED', name, payload})
const actionRejected = (name, error) => ({type: 'PROMISE', status: 'REJECTED', name,  error})

const actionPromise = (name, promise) =>
  async (dispatch) => {
      dispatch(actionPending(name)) 
      try{
          let payload = await promise
          dispatch(actionResolved(name, payload))
          return payload;
      }
      catch(error){
          dispatch(actionRejected(name, error))
      }
  }

 export default actionPromise; 