All files / src/actions utils.js

100% Statements 12/12
100% Branches 2/2
100% Functions 4/4
100% Lines 11/11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 204x 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
      })
  }
}