async-library / react-async

🍾 Flexible promise-based React data loader

Home Page:https://docs.react-async.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

can this be used together with redux-thunk (action creators)?

ronnaf opened this issue · comments

Is something like this an ok thing to do?

export const getUsers = ({ query }, { signal }) => async (dispatch, getState) => {
  const params = getUsersQuery(query)
  const response = await fetch(`${client}/users${params}`, {
    signal,
    method: 'GET',
    headers: {
      authorization: `Bearer TOKEN_HERE`
    }
  })

  const result = await response.json()
  const users = result.docs
  if (Array.isArray(users)) {
    dispatch(receiveUsers({ users }))
  } else {
    throw new Error(users.message || 'Unable to get users!')
  }
}