简体   繁体   English

完成 concatMap 后如何进行另一个 api 调用

[英]How to make another api call after finishing of concatMap

I would like to make an API call after my concatMap is finished.我想在我的 concatMap 完成后进行 API 调用。

from(array)
        .pipe(
           concatMap(el => return APICall(el))
        )
        .subscribe(
           response => { 
            if(!(response instanceof HttpErrorResponse)) {
              responses.push(response);
            }
          }
        ) 

How can I wait untill concatMap finish working and then do other API calls?我怎样才能等到 concatMap 完成工作,然后再进行其他 API 调用?

You could pipe in a switchMap (or a concatMap ) inside the outer concatMap .您可以 pipe 在外部concatMap内的switchMap (或concatMap )中。

from(array).pipe(
  concatMap(el => return APICall(el).pipe(
    switchMap(el2 => return OtherApiCall(el2))
  ))
).subscribe(
  response => { 
    if(!(response instanceof HttpErrorResponse)) {
      responses.push(response);
    }
  }
); 

Update: wait for all concatMap to complete更新:等待所有concatMap完成

forkJoin(
  array.map(el => APICall(el))
).pipe(
  switchMap(res =>        // <-- here `res` would be an array of all responses from the array
    OtherApiCall()
  )
).subscribe(...);

Update 2: wait for all sequential requests to complete更新 2:等待所有顺序请求完成

There isn't a direct way to execute something after all the sequential requests using from + concatMap .在使用from + concatMap的所有顺序请求之后,没有直接的方法来执行某些操作。 You could however use toArray operator to collect all the responses and emit it as an array similar to the forkJoin 's output.但是,您可以使用toArray运算符收集所有响应并将其作为类似于forkJoin的 output 的数组发出。 This would essentially halt the workflow until all the responses are collected.这将基本上停止工作流,直到收集到所有响应。

from(array).pipe(
  concatMap(el => return APICall(el)),
  toArray(),
  switchMap(res =>         // <-- here `res` would be an array of all responses from the array
    return OtherApiCall()
  )
).subscribe(
  response => { 
    if(!(response instanceof HttpErrorResponse)) {
      responses.push(response);
    }
  }
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM