Created
August 12, 2019 11:45
-
-
Save ealipio/1b3f9acfea229ff2c600605cb8fb9f76 to your computer and use it in GitHub Desktop.
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
function request(url) { | |
makeAjaxCall( url, (response) => it.next( response)) | |
} | |
function main() { | |
const result = yield request('http://some.com/1') | |
const data = JSON.parse( result ); | |
const result2 = yield request('http://some.com/2' + data.id); | |
const response = JSON.parse( result2 ); | |
console.log(`The value you asked for is: ${response.value}`) | |
} | |
function* foo(){ | |
let x = yield "please give me a value for x" | |
let y = yield "please give me a value for y" | |
let z = yield "please give me a value for z" | |
return (x+ y +z) | |
} | |
const it = main(); | |
it.next() // get it all started | |
//============ another example: =============== | |
const myFoo = foo(); | |
myFoo.next(1); | |
myFoo.next(2); | |
myFoo.next(3); |
boilerplate saga setup
/**
redux middleware
dispatch redux actions?
handling calling next and pausing, etc.
**/
import { createStore, applyMiddleware} from 'redux';
import createSagaMiddleware from 'redux-saga';
import reduces from './reducers'
import rootsaga from './sagas'
// cretae the saga MiddleWare
const sagaMiddleware = createSagaMiddleware();
// mount it on the Store:
const store = createStore(
reducer,
applyMiddleware(sagaMiddleware)
);
// then run the saga
sagaMiddleware.run(rootSaga)
/** the main saga file is usually split into two different classes of saga: workers & watchers
Watcher saga's see every action that is dispatched to the redux store, if it matches the action they are told to handle, they assign it to their worker saga
**/
// watcher Saga
export function* watchGetProducts() {
yield takeEvery(actions.GET_ALL_PRODUCTS, getAllProducts)
}
// worker saga
export function* getAllProducts() {
const products = yield call(api.getProducts)
yield put(actions.receiveProducts(products))
}
// =========== right side ===========
// watch saga
export function* watchCheckout() {
while(true) {
yield take(actions.CHECKOUT_REQUEST)
yield call(checkout)
}
}
// worker Saga
export function* checkout() {
try {
const cart = yield select(getCart);
yield call(api.buyPRoduct, cart)
yield put(actions.checkoutSuccess(cart))
} catch(error) {
yield put(actions.checkoutFailure(error))
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
from this conference:
https://www.youtube.com/watch?v=o3A9EvMspig