Skip to content

Instantly share code, notes, and snippets.

@evaldosantos
Created July 12, 2018 20:58
Show Gist options
  • Save evaldosantos/a6d936ff1222e79c57d620738d1a9649 to your computer and use it in GitHub Desktop.
Save evaldosantos/a6d936ff1222e79c57d620738d1a9649 to your computer and use it in GitHub Desktop.
export default function createAsyncAction (type, fn) {
return (...args) => async (dispatch) => {
dispatch({
type: `${type}_STARTED`,
payload: args
});
let result;
try {
result = await fn(...args);
} catch (error) {
dispatch({
type: `${type}_FAILED`,
error: true,
payload: error
});
throw error;
}
dispatch({
type: `${type}_ENDED`,
payload: result
});
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment