1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 4x 3x 3x 3x 3x 3x 3x 2x 2x 1x 1x | export const thunkCreator = (action) => { const { types, promise, ...rest } = action const [ REQUESTED, RESOLVED, REJECTED ] = types return (dispatch) => { dispatch({ ...rest, type: REQUESTED }) return promise .then(result => { if (result.error) throw new Error(result.error) dispatch({ ...rest, type: RESOLVED, result }) return result }) .catch(error => { dispatch({ ...rest, type: REJECTED, error }) throw error }) } } |