【问题标题】:How to make multiple ajax call concurrently in redux-observable?如何在 redux-observable 中同时进行多个 ajax 调用?
【发布时间】:2018-02-28 21:47:16
【问题描述】:

我有一个字符串数组schemaNames = [schemaName1, schemaName2 ...],对于schemaNames中的每个元素,我想进行一个restful调用,得到schema,我想得到一个对象的结果。

我想出了一个这样的方法

export const fetchSchemasEpic = action$ =>
  action$.ofType(FETCH_SCHEMAS.REQUEST)
    .map(action => action.schemaNames)
    .switchMap(schemaNames => forkJoin(schemaNames
      .map(schemaName => ajax.getJSON('The API')))
      .map(doFetchSchemasFulfilled));

但似乎这个史诗会返回一个无序数组,所以我无法指定 schemaName 和 schema 之间的对应关系。任何人都可以帮助我吗?非常感谢。

【问题讨论】:

    标签: rxjs redux-observable


    【解决方案1】:

    我找到了解决办法:

    export const fetchSchemasEpic = action$ =>
      action$.ofType(FETCH_SCHEMAS.REQUEST)
        .map(action => action.schemaNames)
        .distinctUntilChanged((old, cur) =>
          old.length === cur.length &&
          old.every((v, i) => v === cur[i]))
        .switchMap(schemaNames => forkJoin(schemaNames
          .map(schemaName => ajax.getJSON(`RESR API`)
            .map((res) => {
              const ret = {};
              ret[schemaName] = res;
              return ret;
            })
            .catch(() => ({}))))
          .map(schemas => Object.assign({}, ...schemas))
          .map(doFetchSchemasFulfilled));
    

    【讨论】:

      猜你喜欢
      • 2019-02-09
      • 2018-11-10
      • 2019-07-21
      • 1970-01-01
      • 2017-04-14
      • 2015-01-29
      • 2021-11-01
      • 1970-01-01
      • 2020-07-14
      相关资源
      最近更新 更多