Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Async in Redux
Search
Darpan Kakadia
October 25, 2017
Technology
0
220
Async in Redux
Slides from my talk at ReactiveConf 2017 on how to handle Async in Redux.
Darpan Kakadia
October 25, 2017
Tweet
Share
Other Decks in Technology
See All in Technology
CDCL による厳密解法を採用した MILP ソルバー
imai448
3
180
個人でもIAM Identity Centerを使おう!(アクセス管理編)
ryder472
4
240
iOS/Androidで同じUI体験をネ イティブで作成する際に気をつ けたい落とし穴
fumiyasac0921
1
110
DynamoDB でスロットリングが発生したとき/when_throttling_occurs_in_dynamodb_short
emiki
0
270
TypeScriptの次なる大進化なるか!? 条件型を返り値とする関数の型推論
uhyo
2
1.7k
100 名超が参加した日経グループ横断の競技型 AWS 学習イベント「Nikkei Group AWS GameDay」の紹介/mediajaws202411
nikkei_engineer_recruiting
1
170
アジャイルチームがらしさを発揮するための目標づくり / Making the goal and enabling the team
kakehashi
3
150
適材適所の技術選定 〜GraphQL・REST API・tRPC〜 / Optimal Technology Selection
kakehashi
1
710
RubyのWebアプリケーションを50倍速くする方法 / How to Make a Ruby Web Application 50 Times Faster
hogelog
3
950
SRE×AIOpsを始めよう!GuardDutyによるお手軽脅威検出
amixedcolor
0
200
なぜ今 AI Agent なのか _近藤憲児
kenjikondobai
4
1.4k
アジャイルでの品質の進化 Agile in Motion vol.1/20241118 Hiroyuki Sato
shift_evolve
0
180
Featured
See All Featured
A better future with KSS
kneath
238
17k
Designing Experiences People Love
moore
138
23k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.1k
Typedesign – Prime Four
hannesfritz
40
2.4k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
RailsConf 2023
tenderlove
29
900
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
The Art of Programming - Codeland 2020
erikaheidi
52
13k
Raft: Consensus for Rubyists
vanstee
136
6.6k
Mobile First: as difficult as doing things right
swwweet
222
8.9k
Transcript
Async in Redux
Darpan Kakadia UI Engineer @Cleartrip @kakadiadarpan
Promises, Thunks or Sagas?
You need just one!
You need just one!
Redux saga is a library which helps you manage async
actions using ES6 generator functions. Redux Saga
import { delay } from 'redux-saga' import { put, takeEvery
} from 'redux-saga/effects' // Our worker Saga: will perform the async increment task export function* incrementAsync() { yield delay(1000) yield put({ type: 'INCREMENT' }) } // Our watcher Saga: spawn a new incrementAsync task on each INCREMENT_ASYNC export function* watchIncrementAsync() { yield takeEvery('INCREMENT_ASYNC', incrementAsync) }
import { delay } from 'redux-saga' import { put, takeEvery
} from 'redux-saga/effects' // Our worker Saga: will perform the async increment task export function* incrementAsync() { yield delay(1000) yield put({ type: 'INCREMENT' }) } // Our watcher Saga: spawn a new incrementAsync task on each INCREMENT_ASYNC export function* watchIncrementAsync() { yield takeEvery('INCREMENT_ASYNC', incrementAsync) }
import { delay } from 'redux-saga' import { put, takeEvery
} from 'redux-saga/effects' // Our worker Saga: will perform the async increment task export function* incrementAsync() { yield delay(1000) yield put({ type: 'INCREMENT' }) } // Our watcher Saga: spawn a new incrementAsync task on each INCREMENT_ASYNC export function* watchIncrementAsync() { yield takeEvery('INCREMENT_ASYNC', incrementAsync) }
Let’s see how Thunks compares with Saga?
function fetchData() { return dispatch => { axios .get("/data") .then(data
=> {dispatch({ type: "FETCH_SUCCESS", data });}) .catch(error => dispatch({ type: "FETCH_FAILED", error })); }; } Redux Thunk
function fetchData() { return dispatch => { axios .get("/data") .then(data
=> {dispatch({ type: "FETCH_SUCCESS", data });}) .catch(error => dispatch({ type: "FETCH_FAILED", error })); }; } Redux Thunk
function fetchData() { return dispatch => { axios .get("/data") .then(data
=> {dispatch({ type: "FETCH_SUCCESS", data });}) .catch(error => dispatch({ type: "FETCH_FAILED", error })); }; } Redux Thunk
function* fetchData() { try { const data = yield call(axios.get,
"/data"); yield put({ type: "FETCH_SUCCESS", data }); } catch (error) { yield put({ type: "FETCH_FAILED", error }); } } function* rootSaga() { yield* takeLatest("FETCH_DATA", fetchData); } Redux Saga
function* fetchData() { try { const data = yield call(axios.get,
"/data"); yield put({ type: "FETCH_SUCCESS", data }); } catch (error) { yield put({ type: "FETCH_FAILED", error }); } } function* rootSaga() { yield* takeLatest("FETCH_DATA", fetchData); } Redux Saga
function* fetchData() { try { const data = yield call(axios.get,
"/data"); yield put({ type: "FETCH_SUCCESS", data }); } catch (error) { yield put({ type: "FETCH_FAILED", error }); } } function* rootSaga() { yield* takeLatest("FETCH_DATA", fetchData); } Redux Saga
function* fetchData() { try { const data = yield call(axios.get,
"/data"); yield put({ type: "FETCH_SUCCESS", data }); } catch (error) { yield put({ type: "FETCH_FAILED", error }); } } function* rootSaga() { yield* takeLatest("FETCH_DATA", fetchData); } Redux Saga
Isn’t that just too much work?
Advantages of redux-saga
• Task cancellation Advantages of redux-saga
• Task cancellation • Race conditions Advantages of redux-saga
• Task cancellation • Race conditions • Throttling Advantages of
redux-saga
• Task cancellation • Race conditions • Throttling • Debouncing
Advantages of redux-saga
• Task cancellation • Race conditions • Throttling • Debouncing
• Retrying network calls Advantages of redux-saga
• Task cancellation • Race conditions • Throttling • Debouncing
• Retrying network calls • Undo actions Advantages of redux-saga
@kakadiadarpan