Instantly share code, notes, and snippets.

Embed
What would you like to do?
eth-hot-wallet Saga.js usage example
/**
* Saga.js usage example
* The mental model is that a saga is like a separate thread in your application
* that's solely responsible for side effects. redux-saga is a redux middleware,
* which means this thread can be started, paused and cancelled from the main
* application with normal redux actions, it has access to the full redux application
* state and it can dispatch redux actions as well.
*/
export function* checkFaucetApi() {
const requestURL = checkFaucetAddress;
try {
// call to external api
const result = yield call(request, requestURL)
// this saga will resume running when call is complete and parsed, as if it was a synchronous code
if (result.message.serviceReady) {
// call to redux action creator
yield put(checkFaucetSuccess());
} else {
yield put(checkFaucetError('faucet not ready'));
}
} catch (err) {
// call redux action creator on error
yield put(checkFaucetError(err));
}
}
// listen to redux actions
export default function* defaultSaga() {
yield takeLatest(CHECK_FAUCET, checkFaucetApi);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment