rsnext/examples/with-redux-saga/saga.js

30 lines
677 B
JavaScript
Raw Normal View History

import { all, call, delay, put, take, takeLatest } from 'redux-saga/effects'
import { actionTypes, failure, loadDataSuccess, tickClock } from './actions'
2017-07-13 20:55:29 +02:00
function* runClockSaga() {
2017-07-13 20:55:29 +02:00
yield take(actionTypes.START_CLOCK)
while (true) {
yield put(tickClock(false))
yield delay(1000)
2017-07-13 20:55:29 +02:00
}
}
function* loadDataSaga() {
2017-07-13 20:55:29 +02:00
try {
const res = yield fetch('https://jsonplaceholder.typicode.com/users')
const data = yield res.json()
yield put(loadDataSuccess(data))
} catch (err) {
yield put(failure(err))
}
}
function* rootSaga() {
2017-07-13 20:55:29 +02:00
yield all([
call(runClockSaga),
takeLatest(actionTypes.LOAD_DATA, loadDataSaga),
2017-07-13 20:55:29 +02:00
])
}
export default rootSaga