Consider I am making a pretty generic api call using redux async action. There is an "INIT"
dispatch and either "SUCCESS"
or "FAILURE"
, regarding of the result.
Does it make any difference to put the "INIT"
dispatch into the try catch
block?
OPTION A (outside the try catch
block):
export const doSomething = data => async dispatch => {
dispatch(doSomethingInit()) // <-- outside try block
try {
let response = await client.get("/api")
if (response) {
dispatch(doSomethingSuccess(response.data))
}
} catch (error) {
dispatch(doSomethingFailure(error))
}
}
OPTION B (inside the try catch
block):
export const doSomething = data => async dispatch => {
try {
dispatch(doSomethingInit()) // <-- inside the try block
let response = await client.get("/api")
if (response) {
dispatch(doSomethingSuccess(response.data))
}
} catch (error) {
dispatch(doSomethingFailure(error))
}
}
I would say it is just a subtle inessential detail, however I do spend a minute thinking about it almost every time I write a new async action… :/