Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs Redux saga多重api调用_Reactjs_Redux_Redux Saga - Fatal编程技术网

Reactjs Redux saga多重api调用

Reactjs Redux saga多重api调用,reactjs,redux,redux-saga,Reactjs,Redux,Redux Saga,我有一帮使用redux saga的api调用的动作观察者。问题是,我想制作一个动作观察程序,它启动所有这些动作观察程序来获取所有api,而不必重复我已经拥有的代码。如果一个观察者返回一个被拒绝的承诺,它应该取消所有其他观察者。最好的方法是什么 function* watchFetchUsers() { while(true) { yield take([FETCH_USERS]); try { const users = yield

我有一帮使用redux saga的api调用的动作观察者。问题是,我想制作一个动作观察程序,它启动所有这些动作观察程序来获取所有api,而不必重复我已经拥有的代码。如果一个观察者返回一个被拒绝的承诺,它应该取消所有其他观察者。最好的方法是什么

function* watchFetchUsers() {
    while(true) {
        yield take([FETCH_USERS]);
        try {
            const users = yield call(api.fetchUserData);
            yield put({ type:FETCH_USERS, payload: users });    
        } catch (err) {
            yield put({ type:SIGNOUT, status: err });
            return err;
        }
    }
}

function* watchFetchDepartments() {
    while(true) {
        yield take([FETCH_DEPARTMENTS]);
        try {
            const departments = yield call(api.fetchDepartmentData);
            yield put({ type:FETCH_DEPARTMENTS, payload: departments });    
        } catch (err) {
            yield put({ type:SIGNOUT, status: err });
            return err;
        }
    }
}

function* watchFetchPositions() {
    while(true) {
        yield take([FETCH_POSITIONS]);
        try {
            const positions = yield call(api.fetchPositionData);
            yield put({ type:FETCH_POSITIONS, payload: positions });    
        } catch (err) {
            yield put({ type:SIGNOUT, status: err });
            return err;
        }
    }
}

function* watchFetchBanks() {
    while(true) {
        yield take([FETCH_BANKS]);
        try {
            const banks = yield call(api.fetchBankData);
            yield put({ type:FETCH_BANKS, payload: banks });    
        } catch (err) {
            yield put({ type:SIGNOUT, status: err });
            return err;
        }
    }
}

function* watchFetchAuthenticatedUser() {
    while(true) {
        yield take([FETCH_AUTHENTICATED_USER]);
        try {
            const user = yield call(api.fetchAuthenticatedUser);
            yield put({ type:FETCH_AUTHENTICATED_USER, payload: user });    
        } catch (err) {
            yield put({ type:SIGNOUT, status: err });
            return err;
        }
    }
}

export default function* fetchData() {
    yield [
        fork(watchFetchUsers),
        fork(watchFetchDepartments),
        fork(watchFetchPositions),
        fork(watchFetchBanks),
        fork(watchFetchAuthenticatedUser)
    ];
}
这个怎么样

export function* watchFetchAll() {
  while(true) {
    // const {type} = yield take(['FETCH_A', 'FETCH_B', ...]);
    const {type} = yield take(action => /^FETCH_/.test(action.type));
    console.log('type %s', type);
    try {
      const data = yield call(api.fetch, type);
      console.log('data', data);
      yield put({type, payload: data})
    }
    catch (error) {
      console.log('error', error);
      yield put({ type: 'SIGNOUT', status: error })
    }
  }
}

export default function* fetchData() {
    yield *watchFetchAll();
}
简单的api实现:

const api = {
  fetch(type) {
    switch (type) {
      case 'FETCH_A': return Promise.resolve({result: 'Fetched A type'});
      case 'FETCH_B': return Promise.resolve({result: 'Fetched B type'});
      // other cases
      default: console.log(`Unknown type ${type}`);
    }
  }
};

分叉任务的错误会传播到父任务。 我不确定下面的内容是否是你想要的。但也许它会起作用

function* watchFetchUsers() {
    while(true) {
        yield take([FETCH_USERS]);
        const users = yield call(api.fetchUserData);
        yield put({ type:FETCH_USERS, payload: users });    
    }
}

function* watchFetchDepartments() {
    while(true) {
        yield take([FETCH_DEPARTMENTS]);
        const departments = yield call(api.fetchDepartmentData);
        yield put({ type:FETCH_DEPARTMENTS, payload: departments });    
    }
}

function* watchFetchPositions() {
    while(true) {
        yield take([FETCH_POSITIONS]);
        const positions = yield call(api.fetchPositionData);
        yield put({ type:FETCH_POSITIONS, payload: positions });    
    }
}

function* watchFetchBanks() {
    while(true) {
        yield take([FETCH_BANKS]);
        const banks = yield call(api.fetchBankData);
        yield put({ type:FETCH_BANKS, payload: banks });    
    }
}

function* watchFetchAuthenticatedUser() {
    while(true) {
        yield take([FETCH_AUTHENTICATED_USER]);
        const user = yield call(api.fetchAuthenticatedUser);
        yield put({ type:FETCH_AUTHENTICATED_USER, payload: user });   
    }
}

export default function* fetchData() {
    while (true) {
        let tasks;
        try {
           tasks = yield [
                fork(watchFetchUsers),
                fork(watchFetchDepartments),
                fork(watchFetchPositions),
                fork(watchFetchBanks),
                fork(watchFetchAuthenticatedUser)
            ];
            yield join(...tasks)
        } catch (e) {
           yield cancel(...tasks);
           yield put({ type:SIGNOUT, status: err });
        }
    }
}
或者,如果您不想恢复任务

//....
export default function* fetchData() {
    try {
        yield [
            fork(watchFetchUsers),
            fork(watchFetchDepartments),
            fork(watchFetchPositions),
            fork(watchFetchBanks),
            fork(watchFetchAuthenticatedUser)
        ];
    } catch (e) {
        yield put({ type:SIGNOUT, status: err });
    }
}