Skip to content

Instantly share code, notes, and snippets.

Created August 12, 2019 11:45
What would you like to do?
function request(url) {
makeAjaxCall( url, (response) => response))
function main() {
const result = yield request('')
const data = JSON.parse( result );
const result2 = yield request('' +;
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(); // get it all started
//============ another example: ===============
const myFoo = foo();;;;
Copy link

ealipio commented Aug 12, 2019

Copy link

ealipio commented Aug 12, 2019

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(

// then run the saga

Copy link

ealipio commented Aug 12, 2019

/** 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