Skip to content

Instantly share code, notes, and snippets.

@safronman
Last active February 19, 2023 14:45
Show Gist options
  • Select an option

  • Save safronman/1cf66c1c4128b33398f7ca707322d565 to your computer and use it in GitHub Desktop.

Select an option

Save safronman/1cf66c1c4128b33398f7ca707322d565 to your computer and use it in GitHub Desktop.
thunkTryCatch
export const changeCourseId = createAsyncThunk(
'auth/change-course',
async (payload: PayloadType, thunkAPI) => {
return thunkTryCatch(thunkAPI, async () => {
await packsApi.put(payload)
})
}
)
import { hostAppActions } from '../../app/host-app-slice'
export const thunkTryCatch = async (thunkAPI: any, logic: Function) => {
try {
return await logic()
} catch (e: any) {
const error = e.response ? e.response.data.error : e.message + ', more details in the console'
// thunkAPI.dispatch(appThunks.setAppError({ error }))
thunkAPI.dispatch(hostAppActions.showSnackbar({ message: error, severity: 'error' }))
return thunkAPI.rejectWithValue({ error })
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment