Skip to content

Instantly share code, notes, and snippets.

@dewey92
Last active April 22, 2018 10:29
Show Gist options
  • Select an option

  • Save dewey92/dae041962972847efee3ba3633e2dae6 to your computer and use it in GitHub Desktop.

Select an option

Save dewey92/dae041962972847efee3ba3633e2dae6 to your computer and use it in GitHub Desktop.
Async Action Creators
const createAction = actionName => Object.assign(
payload => ({
type: actionName,
payload
}),
{ type: actionName } // <- Inject `type` attribute to the action func
)
const createAsyncAction = actionName => ({
request: createAction(actionName + '_REQUEST'),
success: createAction(actionName + '_SUCCESS'),
failed: createAction(actionName + '_FAILED'),
})
const getUser = createAsyncAction('GET_USER')
const getPost = createAsyncAction('GET_POST')
// Contoh cara pemakaiannya:
//
// getPost.request(99)
// getPost.success({ id: 99, title: 'Aku ganteng' })
// getPost.failed({ error: 'Maaf, Anda kurang ganteng' })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment