在 Redux Saga 中顺序调用操作(同步)

Call actions sequentially in Redux Saga (synchronously)

我想在进行一些更改后刷新项目列表,它们是要命中的不同端点,并且它们需要同步(我想在获得第一个函数响应后调用第二个函数)。

收到 inviteUsers() 操作的响应后,我想调用 getAllUsers() 操作来获取更新后的列表。

我的问题是,处理这些顺序调用的最佳做法是什么?我应该添加另一个 saga 来监听 INVITE_USERS_SUCCESS 然后调用第二个动作,还是应该像这样在 inviteUsers() 动作中调用 getAllUsers() 动作?:

// first action
function* inviteUsers(args) {
    try {
        yield call(() => API.post('/users/invite', args));

        yield put({
            type: CustomConstants.INVITE_USERS_SUCCESS
        });

        // call second action
        yield call(getAllInvites);
    } catch (e) {
        yield put({
            type: CustomConstants.INVITE_USERS_FAILURE,
            error: e,
        });
    }
}

// second action (after first action response)
function* getAllInvites() {
    try {
        yield call(() => API.get('/users/all-invites'));

        yield put({
            type: CustomConstants.LIST_PENDING_INVITES_SUCCESS
        });
    } catch (e) {
        yield put({
            type: CustomConstants.LIST_PENDING_INVITES_FAILURE,
            error: e,
        });
    }
}

Should I add another saga to listen for the INVITE_USERS_SUCCESS and then call the second action

是的,你应该。当您只使用操作来触发它们,更一般地说,让它们进行通信(这是事件驱动的开发)时,您会充分利用 Redux-saga。

这让您可以编写更多代码,也可以开发独立的 sagas。

or should I call the getAllUsers() action inside the inviteUsers() action

我不建议你这样做,因为你的 inviteUsers saga 不会与 getAllUsers 的 saga 分离(因为它们是更大流程的一部分)而且你不会当您需要不同的流程时,可以单独使用它们。

如果您需要更多帮助,请告诉我