Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Async in Redux

Async in Redux

Slides from my talk at ReactiveConf 2017 on how to handle Async in Redux.

Darpan Kakadia

October 25, 2017
Tweet

Other Decks in Technology

Transcript

  1. Redux saga is a library which helps you manage async

    actions using ES6 generator functions. Redux Saga
  2. 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) }
  3. 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) }
  4. 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) }
  5. function fetchData() { return dispatch => { axios .get("/data") .then(data

    => {dispatch({ type: "FETCH_SUCCESS", data });}) .catch(error => dispatch({ type: "FETCH_FAILED", error })); }; } Redux Thunk
  6. function fetchData() { return dispatch => { axios .get("/data") .then(data

    => {dispatch({ type: "FETCH_SUCCESS", data });}) .catch(error => dispatch({ type: "FETCH_FAILED", error })); }; } Redux Thunk
  7. function fetchData() { return dispatch => { axios .get("/data") .then(data

    => {dispatch({ type: "FETCH_SUCCESS", data });}) .catch(error => dispatch({ type: "FETCH_FAILED", error })); }; } Redux Thunk
  8. 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
  9. 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
  10. 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
  11. 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
  12. • Task cancellation • Race conditions • Throttling • Debouncing

    • Retrying network calls Advantages of redux-saga
  13. • Task cancellation • Race conditions • Throttling • Debouncing

    • Retrying network calls • Undo actions Advantages of redux-saga