stream-saga/src/js/sagas/api-saga.js

25 lines
562 B
JavaScript
Raw Normal View History

2019-06-21 00:06:42 -04:00
import { takeEvery, call, put } from 'redux-saga/effects'
2019-06-19 22:37:59 -04:00
export default function* watcherSaga() {
2019-06-21 00:06:42 -04:00
yield takeEvery("DATA_REQUESTED", workerSaga)
2019-06-19 22:37:59 -04:00
}
function* workerSaga() {
2019-06-21 00:06:42 -04:00
try {
const payload = yield call(getData)
yield put({ type: "DATA_LOADED", payload })
} catch (e) {
yield put({ type: "API_ERRORED", payload: e })
}
2019-06-19 22:37:59 -04:00
}
function getData() {
2019-06-21 00:59:25 -04:00
// return fetch("http://localhost:3030/api/twitter?hashtag=tesla").then(res1 =>
// res1.json()
2019-06-21 00:06:42 -04:00
// )
2019-06-21 00:59:25 -04:00
return fetch("https://jsonplaceholder.typicode.com/posts").then(response =>
response.json()
)
2019-06-19 22:37:59 -04:00
}