Last active
April 29, 2019 17:48
-
-
Save PaulLaux/c6a0cddf0bdb84670df4e539bce9b1f6 to your computer and use it in GitHub Desktop.
eth-hot-wallet Saga.js usage example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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