123456789101112131415161718192021222324252627282930313233343536 |
- import {all, takeEvery, put, call} from 'redux-saga/effects';
- export function promiseReducer(state = {}, { type, status, payload, error, name }) {
- if (type === 'PROMISE') {
- return {
- ...state,
- [name]: { status, payload, error }
- }
- }
- return state;
- }
- const actionPending = name => ({ type: 'PROMISE', status: 'PENDING', name })
- const actionResolved = (name, payload) => ({ type: 'PROMISE', status: 'RESOLVED', name, payload })
- const actionRejected = (name, error) => ({ type: 'PROMISE', status: 'REJECTED', name, error })
- export const actionPromise = (name, promise) =>
- ({type: 'PROMISE_START', name, promise})
- export function* promiseWorker(action){ //это типа actionPromise который thunk
- const {name, promise} = action
- yield put(actionPending(name))
- try {
- let data = yield promise
- yield put(actionResolved(name, data))
- return data
- }
- catch (error) {
- yield put(actionRejected(name, error))
- }
- }
- export function* promiseWatcher(){
- yield takeEvery('PROMISE_START', promiseWorker)
- }
|