【问题标题】:rxjs - making requests in parallel and nested requestsrxjs - 并行和嵌套请求发出请求
【发布时间】:2021-09-14 19:49:06
【问题描述】:

您好,我想基于数组并行发出请求 - 如下

1) 执行单一登录请求 2)对于基于iif条件的每个条目,取iif的结果并执行一系列操作 3)基于链操作的最终结果的补丁状态。

我试过的代码如下


 this.questionAnswerService
            .loginToQuriousApi()
            .toPromise()
            .then((response) => {
                access_token = response.access_token;
                console.log(access_token);
                entries.forEach((entry) => {
                    return iif(
                        () => entry.extension == 'pdf',
                        defer(() => this.convertFile.getPdfContentInText(entry.id)),
                        defer(() => this.convertFile.getTextContent(entry.id))
                    ).pipe(
                        concatMap((textContent: string) => {
                            console.log(textContent);
                            ctx.patchState({
                                qnaloading: true,
                            });
                            return this.convertFile.convertFileContent(textContent);
                        }),
                        concatMap((convertedObject) => {
                            return this.convertFile.uploadToQNA(
                                entry.id,
                                convertedObject,
                                access_token
                            );
                        }),
                        concatMap((response: any) => {
                            ctx.patchState({ loading: false });
                            return ctx.dispatch(new UpdateEntries([response.fileEntry]));
                        }),
  
                        catchError((e) => {
                            ctx.patchState({ loading: false });
                            return of('reject: ' + JSON.stringify(e));
                        })
                    );
                });
            })
            .catch((e) => {
                ctx.patchState({ loading: false });
                return console.log('reject: ' + JSON.stringify(e));
            });

这里的条目是一个对象数组。承诺会被执行,但 for 循环不会。

【问题讨论】:

  • 在循环中使用。例如: for (const key in object) { if (Object.hasOwnProperty.call(object, key)) { const element = object[key]; } }

标签: angular rxjs


【解决方案1】:

iif 生成一个未订阅的 observable。这是未触发请求的原因之一。你可以这样重写:

this.questionAnswerService
  .loginToQuriousApi()
  .pipe(
    tap(() => ctx.patchState({ qnaloading: true }))
    map((res) => res.access_token),
    switchMap((access_token) => forkJoin(buildTasks(entries, access_token))),
    finalize(() => ctx.patchState({ qnaloading: false }))
  ).subscribe(); // not a big fan of the empty subscribe, but everything seems to be happening already in the other operations

还有buildTasks 方法:

private buildTasks(entries: any[], access_token: string): Observable<any>[] {
  return entries.map((entry) =>
    iif(
      () => entry.extension === 'pdf',
      defer(() => this.convertFile.getPdfContentInText(entry.id)),
      defer(() => this.convertFile.getTextContent(entry.id))
    ).pipe(
      concatMap((textContent: string) => {
        console.log(textContent);
        return this.convertFile.convertFileContent(textContent);
      }),
      concatMap((convertedObject) => {
        return this.convertFile.uploadToQNA(
          entry.id,
          convertedObject,
          access_token
        );
      }),
      tap((response: any) => ctx.dispatch(new UpdateEntries([response.fileEntry])),
      catchError((e) => {
        return of('reject: ' + JSON.stringify(e));
      })
    )
  ));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-24
    • 2017-08-23
    • 2020-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多