redux-saga-callback
In a normal flow of a redux-saga application there might be some cases that you want get notified when a saga triggered by a dispatched action is completed. The purpose of this library is to provide some helper functions to achieve that functionalities.
Install
npm i redux-saga-callback
Usage
;
Wrap your saga generator with withCallback
;
Defined callback in your action if you need
In saga you can wait for it (no callback definition is needed with putWait)
const users = ;
API
withCallback
This is a higer order saga which will call the onComplete callback property of the action if defined.
onComplete
- error: error which is thrown by the saga
- cancelled: Will be true if the saga is cancelled
- data: the data returned by the saga
{ /* handle callback */}
{ const users = ; // put users to store ; // returned value will be passed to onComplete function as parameter // Exceptions will be handled by the withCallback and will also be passed to onComplete return users;} { } // userSaga.js
// Component to list usersconst Users = { const isLoading setIsLoading = ; const users setUsers = ; const dispatch = ; { ; if !err && !cancelled ; } ; return isLoading ? 'Loading' : <ul> users </ul> ;}; // Users.jsx
putWait
An effect that dispatches the action (same as put effect) which you can yield and wait for that saga to be completed only if the saga is created using withCallback higher order saga
Example
{ const currentUserId = ; let users = ; if!users // waits until fetchUsers saga is completed // fetchUser saga is defined in userSaga.js above users = ; return users;} { } // userData.js