SlideShare a Scribd company logo
Better react/redux apps
using redux-saga
By @younes Front end Developer @
what about you ?
React Or …
Redux Or …
Thunk Or …
Saga Or …
Opinionated !
Redux !
Better react/redux apps using redux-saga
Better react/redux apps using redux-saga
deterministic part
non-deterministic part
Better react/redux apps using redux-saga
Seriously !
function makeSandwichesForEverybody() {
return function (dispatch, getState) {
if (!getState().sandwiches.isShopOpen) {
// You don’t have to return Promises, but it’s a handy convention
// so the caller can always call .then() on async dispatch result.
return Promise.resolve();
}
// We can dispatch both plain object actions and other thunks,
// which lets us compose the asynchronous actions in a single flow.
return dispatch(
makeASandwichWithSecretSauce('My Grandma')
).then(() =>
Promise.all([
dispatch(makeASandwichWithSecretSauce('Me')),
dispatch(makeASandwichWithSecretSauce('My wife'))
])
).then(() =>
dispatch(makeASandwichWithSecretSauce('Our kids'))
).then(() =>
dispatch(getState().myMoney > 42 ?
withdrawMoney(42) :
apologize('Me', 'The Sandwich Shop')
)
);
};
}
export default function* onboarding() {
yield take(GO_TO_NEXT_STAGE);
// stage: initial-confirmed
yield delay(1000);
yield put(goToNextStage());
// stage: keys-introduced
yield handleKeyExperiments();
yield put(goToNextStage());
yield delay(2000);
yield put(goToNextStage());
// stage: pad-introduced
yield handlePadExperiments();
yield put(goToNextStage());
yield delay(2000);
yield put(goToNextStage());
// stage: control-panel-introduced
yield delay(5000);
yield put(goToNextStage());
yield delay(2000);
// Persist a flag in localStorage, so that this user does not
// have to go through the onboarding flow again.
localStorage.setItem(ONBOARDING_COMPLETED_FLAG, true);
yield put(completeOnboarding());
}
Generators !
function* generator() {
yield 1;
console.log("hello");
const val = test(yield { name: "younes" })
// setting variable value from outside
console.log(val); // 5
}
const iterator = generator();
// Iterator (object with next method)
iterator.next();
// { value: 1, done: false }
iterator.next();
// { value: { name: 'younes' }, done: false }
console.log("random thing");
function delay(time = 3000, returnValue) {
return new Promise(function(resolve) {
setTimeout(resolve.bind(null, returnValue), time);
});
}
function* generator() {
let result = yield delay(2000, "return value");
console.log(result);
result = yield delay(2000);
console.log(result);
}
function runtime(generator) {
function next(nextVal) {
const { value, done } = iterator.next(nextVal);
if (done) {
console.log("done");
return Promise.resolve();
}
if (typeof value.then === "function") {
return Promise.resolve(value.then(next));
}
}
const iterator = generator();
return next(iterator);
}
runtime(generator);
function delay(time = 3000, returnValue) {
return new Promise(function(resolve) {
setTimeout(resolve.bind(null, returnValue), time);
});
}
function* generator() {
let result = yield delay(2000, "return value");
console.log(result);
result = yield delay(2000);
console.log(result);
}
function runtime(generator) {
function next(nextVal) {
const { value, done } = iterator.next(nextVal);
if (done) {
console.log("done");
return Promise.resolve();
}
if (typeof value.then === "function") {
return Promise.resolve(value.then(next));
}
}
const iterator = generator();
return next(iterator);
}
runtime(generator);
function delay(time = 3000, returnValue) {
return new Promise(function(resolve) {
setTimeout(resolve.bind(null, returnValue), time);
});
}
function* generator() {
let result = yield delay(2000, "return value");
console.log(result);
result = yield delay(2000);
console.log(result);
}
function runtime(generator) {
function next(nextVal) {
const { value, done } = iterator.next(nextVal);
if (done) {
console.log("done");
return Promise.resolve();
}
if (typeof value.then === "function") {
return Promise.resolve(value.then(next));
}
}
const iterator = generator();
return next(iterator);
}
runtime(generator);
• Runtime for our generators
Redux-saga
• Runtime for our generators
• Helpers (aka commands) for managing various operations
Redux-saga
• Runtime for our generators
• Helpers (aka commands) for managing various operations
• Integrating redux external APIs (channels)
Redux-saga
Show me some code !
Execution tree
sagamiddleware.run
Worker API
Auth Saga
Messages Feed APIs
Fork
takeLatest
Wait for
ActionX
Race
Execution tree
sagamiddleware.run
Worker API
Auth Saga
Messages Feed APIs
sagamiddleware.run
Admin
Panel
Fork
takeLatest
Wait for
ActionX
Race
Lazy
loaded
Testing ?
Channels
Channels
• Buffering (waiting dropping upcoming actions)
Channels
• Buffering (waiting dropping upcoming actions)
• Communication between sagas
Channels
• Buffering (waiting dropping upcoming actions)
• Communication between sagas
• Communication with external APIs (firebase webosocket
browser APIs …)
Channels
• Buffering (waiting dropping upcoming actions)
• Communication between sagas
• Communication with external APIs (firebase webosocket
browser APIs …)
• Abstracting platform code for sharing code react/react-
native (geolocation ??)
• Keep react strictly a view layer (stateless as much as
possible)

• Keep redux away for the how part

• separate what you need from how you get (yes redux
saga)
Thank you
_younesmln
hello@younesmln.me

More Related Content

PDF
Async History - javascript
PPTX
Process control nodejs
PDF
Async JavaScript in ES7
PDF
The Ring programming language version 1.9 book - Part 92 of 210
PDF
Redux Sagas - React Alicante
PDF
The Ring programming language version 1.9 book - Part 71 of 210
PDF
The Ring programming language version 1.6 book - Part 64 of 189
PDF
The Ring programming language version 1.7 book - Part 85 of 196
Async History - javascript
Process control nodejs
Async JavaScript in ES7
The Ring programming language version 1.9 book - Part 92 of 210
Redux Sagas - React Alicante
The Ring programming language version 1.9 book - Part 71 of 210
The Ring programming language version 1.6 book - Part 64 of 189
The Ring programming language version 1.7 book - Part 85 of 196

What's hot (20)

PDF
State managment in a world of hooks
PPTX
Asynchronous programming from Xamarin Hakcday in Melbourne
PDF
React Back to the Future
PPTX
JS Objects manipulation
PDF
Async Testing giving you a sinking feeling
KEY
Better Together: Building Scalable Real Time Collaborative Apps with Node.js
PDF
The Ring programming language version 1.10 book - Part 74 of 212
PDF
The Ring programming language version 1.7 book - Part 65 of 196
DOCX
7th lab
PDF
The Ring programming language version 1.8 book - Part 67 of 202
PDF
The evolution of java script asynchronous calls
PDF
The Ring programming language version 1.5.1 book - Part 58 of 180
PDF
Automation in angular js
PDF
The Ring programming language version 1.2 book - Part 43 of 84
PDF
Kivy Talk Python Meetup Innsbruck 2017.04.25
PDF
The Ring programming language version 1.10 book - Part 72 of 212
PDF
The Ring programming language version 1.5.3 book - Part 72 of 184
PDF
The Ring programming language version 1.7 book - Part 67 of 196
PPTX
[Android] PLAYING WITH FRAGMENT
PDF
Trisha gee concurrentprogrammingusingthedisruptor
State managment in a world of hooks
Asynchronous programming from Xamarin Hakcday in Melbourne
React Back to the Future
JS Objects manipulation
Async Testing giving you a sinking feeling
Better Together: Building Scalable Real Time Collaborative Apps with Node.js
The Ring programming language version 1.10 book - Part 74 of 212
The Ring programming language version 1.7 book - Part 65 of 196
7th lab
The Ring programming language version 1.8 book - Part 67 of 202
The evolution of java script asynchronous calls
The Ring programming language version 1.5.1 book - Part 58 of 180
Automation in angular js
The Ring programming language version 1.2 book - Part 43 of 84
Kivy Talk Python Meetup Innsbruck 2017.04.25
The Ring programming language version 1.10 book - Part 72 of 212
The Ring programming language version 1.5.3 book - Part 72 of 184
The Ring programming language version 1.7 book - Part 67 of 196
[Android] PLAYING WITH FRAGMENT
Trisha gee concurrentprogrammingusingthedisruptor
Ad

Similar to Better react/redux apps using redux-saga (20)

PDF
Promises are so passé - Tim Perry - Codemotion Milan 2016
PDF
Promise is a Promise
PDF
The evolution of redux action creators
PDF
Promises - Asynchronous Control Flow
PDF
Promise: async programming hero
PDF
Callbacks, promises, generators - asynchronous javascript
PDF
Angular promises and http
PPTX
AngularJS, More Than Directives !
PDF
Side effects-con-redux
PPTX
The Promised Land (in Angular)
PPTX
Async all around us (promises)
PDF
A promise is a Promise
PDF
Recompacting your react application
PDF
Why react matters
PDF
$q and Promises in AngularJS
PDF
React lecture
PDF
Think Async: Asynchronous Patterns in NodeJS
PPTX
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
PPTX
Angular2 rxjs
PPTX
Grails transactions
Promises are so passé - Tim Perry - Codemotion Milan 2016
Promise is a Promise
The evolution of redux action creators
Promises - Asynchronous Control Flow
Promise: async programming hero
Callbacks, promises, generators - asynchronous javascript
Angular promises and http
AngularJS, More Than Directives !
Side effects-con-redux
The Promised Land (in Angular)
Async all around us (promises)
A promise is a Promise
Recompacting your react application
Why react matters
$q and Promises in AngularJS
React lecture
Think Async: Asynchronous Patterns in NodeJS
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Angular2 rxjs
Grails transactions
Ad

Recently uploaded (20)

PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
PDF
737-MAX_SRG.pdf student reference guides
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
UNIT 4 Total Quality Management .pptx
PDF
III.4.1.2_The_Space_Environment.p pdffdf
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Safety Seminar civil to be ensured for safe working.
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
web development for engineering and engineering
PPTX
Sustainable Sites - Green Building Construction
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
R24 SURVEYING LAB MANUAL for civil enggi
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
737-MAX_SRG.pdf student reference guides
CYBER-CRIMES AND SECURITY A guide to understanding
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
CH1 Production IntroductoryConcepts.pptx
UNIT 4 Total Quality Management .pptx
III.4.1.2_The_Space_Environment.p pdffdf
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Model Code of Practice - Construction Work - 21102022 .pdf
Safety Seminar civil to be ensured for safe working.
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Operating System & Kernel Study Guide-1 - converted.pdf
bas. eng. economics group 4 presentation 1.pptx
web development for engineering and engineering
Sustainable Sites - Green Building Construction

Better react/redux apps using redux-saga

  • 1. Better react/redux apps using redux-saga By @younes Front end Developer @
  • 2. what about you ? React Or … Redux Or … Thunk Or … Saga Or …
  • 10. function makeSandwichesForEverybody() { return function (dispatch, getState) { if (!getState().sandwiches.isShopOpen) { // You don’t have to return Promises, but it’s a handy convention // so the caller can always call .then() on async dispatch result. return Promise.resolve(); } // We can dispatch both plain object actions and other thunks, // which lets us compose the asynchronous actions in a single flow. return dispatch( makeASandwichWithSecretSauce('My Grandma') ).then(() => Promise.all([ dispatch(makeASandwichWithSecretSauce('Me')), dispatch(makeASandwichWithSecretSauce('My wife')) ]) ).then(() => dispatch(makeASandwichWithSecretSauce('Our kids')) ).then(() => dispatch(getState().myMoney > 42 ? withdrawMoney(42) : apologize('Me', 'The Sandwich Shop') ) ); }; }
  • 11. export default function* onboarding() { yield take(GO_TO_NEXT_STAGE); // stage: initial-confirmed yield delay(1000); yield put(goToNextStage()); // stage: keys-introduced yield handleKeyExperiments(); yield put(goToNextStage()); yield delay(2000); yield put(goToNextStage()); // stage: pad-introduced yield handlePadExperiments(); yield put(goToNextStage()); yield delay(2000); yield put(goToNextStage()); // stage: control-panel-introduced yield delay(5000); yield put(goToNextStage()); yield delay(2000); // Persist a flag in localStorage, so that this user does not // have to go through the onboarding flow again. localStorage.setItem(ONBOARDING_COMPLETED_FLAG, true); yield put(completeOnboarding()); }
  • 13. function* generator() { yield 1; console.log("hello"); const val = test(yield { name: "younes" }) // setting variable value from outside console.log(val); // 5 } const iterator = generator(); // Iterator (object with next method) iterator.next(); // { value: 1, done: false } iterator.next(); // { value: { name: 'younes' }, done: false } console.log("random thing");
  • 14. function delay(time = 3000, returnValue) { return new Promise(function(resolve) { setTimeout(resolve.bind(null, returnValue), time); }); } function* generator() { let result = yield delay(2000, "return value"); console.log(result); result = yield delay(2000); console.log(result); } function runtime(generator) { function next(nextVal) { const { value, done } = iterator.next(nextVal); if (done) { console.log("done"); return Promise.resolve(); } if (typeof value.then === "function") { return Promise.resolve(value.then(next)); } } const iterator = generator(); return next(iterator); } runtime(generator);
  • 15. function delay(time = 3000, returnValue) { return new Promise(function(resolve) { setTimeout(resolve.bind(null, returnValue), time); }); } function* generator() { let result = yield delay(2000, "return value"); console.log(result); result = yield delay(2000); console.log(result); } function runtime(generator) { function next(nextVal) { const { value, done } = iterator.next(nextVal); if (done) { console.log("done"); return Promise.resolve(); } if (typeof value.then === "function") { return Promise.resolve(value.then(next)); } } const iterator = generator(); return next(iterator); } runtime(generator);
  • 16. function delay(time = 3000, returnValue) { return new Promise(function(resolve) { setTimeout(resolve.bind(null, returnValue), time); }); } function* generator() { let result = yield delay(2000, "return value"); console.log(result); result = yield delay(2000); console.log(result); } function runtime(generator) { function next(nextVal) { const { value, done } = iterator.next(nextVal); if (done) { console.log("done"); return Promise.resolve(); } if (typeof value.then === "function") { return Promise.resolve(value.then(next)); } } const iterator = generator(); return next(iterator); } runtime(generator);
  • 17. • Runtime for our generators Redux-saga
  • 18. • Runtime for our generators • Helpers (aka commands) for managing various operations Redux-saga
  • 19. • Runtime for our generators • Helpers (aka commands) for managing various operations • Integrating redux external APIs (channels) Redux-saga
  • 20. Show me some code !
  • 21. Execution tree sagamiddleware.run Worker API Auth Saga Messages Feed APIs Fork takeLatest Wait for ActionX Race
  • 22. Execution tree sagamiddleware.run Worker API Auth Saga Messages Feed APIs sagamiddleware.run Admin Panel Fork takeLatest Wait for ActionX Race Lazy loaded
  • 25. Channels • Buffering (waiting dropping upcoming actions)
  • 26. Channels • Buffering (waiting dropping upcoming actions) • Communication between sagas
  • 27. Channels • Buffering (waiting dropping upcoming actions) • Communication between sagas • Communication with external APIs (firebase webosocket browser APIs …)
  • 28. Channels • Buffering (waiting dropping upcoming actions) • Communication between sagas • Communication with external APIs (firebase webosocket browser APIs …) • Abstracting platform code for sharing code react/react- native (geolocation ??)
  • 29. • Keep react strictly a view layer (stateless as much as possible) • Keep redux away for the how part • separate what you need from how you get (yes redux saga)