api fetch saga

Solutions on MaxInterview for api fetch saga by the best coders in the world

showing results for - "api fetch saga"
Fares
21 Jul 2018
1import { takeLatest, call, put } from 'redux-saga/effects';
2
3// watcher saga: watches for actions dispatched to the store, starts worker saga
4export function* watcherSaga() {
5    yield takeLatest('FETCH_CUSTOMERS', workerSaga);
6}
7
8// worker saga: makes the api call when watcher saga sees the action
9function* workerSaga() {
10    try {
11        const response = yield call(fetch, 'https://mydomain/api/customers');
12        const customers = response.data.customers;
13
14        // dispatch a success action to the store with the customers
15        yield put({ type: 'CUSTOMERS_FETCHED', customers });
16    } catch (error) {
17        // dispatch a failure action to the store with the error
18        yield put({ type: 'CUSTOMERS_FETCH_ERROR', error });
19    }
20}