Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

makeSimpleAsync, perform and their problems #52

Open
tomhoule opened this issue Aug 2, 2017 · 3 comments
Open

makeSimpleAsync, perform and their problems #52

tomhoule opened this issue Aug 2, 2017 · 3 comments

Comments

@tomhoule
Copy link
Contributor

tomhoule commented Aug 2, 2017

We pretty much decided we were not going to go forward with makeSimpleAsync because it's too inflexible and does not compose well with other sagas.

The other idea was perform, which is a very basic saga that just dispatches the _SUCCESS or _FAILURE action and uses an optional error handler.

The problem is that perform has to signal the success or error to the parent saga, which can be done with an exception:

try {
  const response = yield perform(call(api.books, action.payload), action)
  ...do something
} catch (_) { }

or with a value

const response = yield perform(call(api.books, action.payload), action)
if (response) {
  ...do something
}

Both methods are ugly and more verbose than they need to be. What we could do instead is wrap the parent saga in another one that dispatches the _SUCCESS and _FAILURE action and handle errors.

The only responsibility of the child saga is then to signal the result by returning a success value or throwing an error.

@fnune
Copy link
Contributor

fnune commented Aug 2, 2017

And that parent saga would be a wrapped re-export of takeLatest, takeEvery, etc. right?

@tomhoule
Copy link
Contributor Author

tomhoule commented Aug 2, 2017

We can provide these as helpers but you can use that saga by itself.

@tomhoule
Copy link
Contributor Author

tomhoule commented Aug 25, 2017

Something like:

export function buckle(saga, onError) {
  return function* inner(action) {
  const success = action.meta.success || `${action.type}_SUCCESS`
  const failure = action.meta.failure || `${action.type}_FAILURE`
  try {
    const result = yield call(saga, action)
    yield put({ type: success, payload: result })
  } catch (err) {
    yield call(onError, failure, err)
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants