首页 > 解决方案 > 等待多个可观察的 ajax 调用完成

问题描述

在 React Native 项目中使用 redux-observables,我正在尝试进行 ajax 调用来检索消息数组。一旦这个调用完成,我想对数组中的每个项目进行单独的 Ajax 调用,等待它们全部完成,然后使用 redux 操作发布它们。

我曾尝试使用 forkjoin,但我正在努力将数组转换为 observables 数组(我认为这是必要的步骤?)并创建单独的 Ajax 调用,然后等待它们完成。这甚至可能吗?

export const messagesEpic: Epic = (action$, state$) =>
    action$.pipe(
        ofType('MESSAGES_FETCH'),
        mergeMap((_: Action) =>
            constructAPICall(
                state$,
                'GET',
                api.endpoints.messages('type', 0),
            ).pipe(
                map(response => < insert transform array to observables and ajax -> wait for all to finish >,
                    catchError(error => of({
                        type: GENERAL_API_ERROR,
                        payload: error.xhr.response,
                        error: true,
                    }))
                )))
    );

在第一次 ajax 调用中收到的数组是一个简单的 json 数组,其中包含包含 id 和其他信息的普通对象。

标签: reactjsreduxrxjsobservableredux-observable

解决方案


推荐阅读