Skip to content

Instantly share code, notes, and snippets.

@joedski
Last active September 19, 2017 01:51
Show Gist options
  • Save joedski/4634f14d9d1dd0568a542c0ffdd7afa2 to your computer and use it in GitHub Desktop.
Save joedski/4634f14d9d1dd0568a542c0ffdd7afa2 to your computer and use it in GitHub Desktop.
hof thunks: resource thunk creator
export function resource(resourceName, mapArgs) {
return (params, meta) => async (getState, dispatch) => {
const fetchOptions = mapArgs(params, meta);
dispatch(resourceActions.requestResource({
name: resourceName,
params,
meta,
}));
try {
const result = await fetch(fetchOptions.uri, fetchOptions.options);
dispatch(resourceActions.receiveResource({
name: resourceName,
params,
meta,
resource: result,
}));
return { result };
}
catch (error) {
dispatch(resourceActions.rejectResource({
name: resourceName,
params,
meta,
error,
}));
return { error };
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment