export default function actionPromise(name, promise) {
    const actionPending = (name) => ({
        type: "PROMISE",
        status: "PENDING",
        name,
    });
    
    const actionFulfilled = (name, payload) => ({
        type: "PROMISE",
        status: "FULFILLED",
        name,
        payload,
    });
    
    const actionRejected = (name, error) => ({
        type: "PROMISE",
        status: "REJECTED",
        name,
        error,
    });

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