想要使用Angular/Ionic中的forkjoin以批量格式调用多个API调用

发布于 2025-02-11 21:53:55 字数 764 浏览 1 评论 0 原文

我有一系列请求:

let array = [req1,req2,req3.....,req(n)]

目前,我仅在获得所有请求响应后才使用forkjoin致电订户。现在,我想修改代码以批量拨打API调用,但是只有在完成所有批次响应后才致电订户,这是可能的吗?

示例

public demoCalls() {
       let demoForkJoin
       var a = [req1,req2,....,req45]
       a.map((data,index)=>{
            demoFrokJoin[index] = this.http.post()
       })
       forkJoin(demoForkJoin)

}

现在而不是这个,我想在批处理中致电10-10 req。 1000ms之后,每10个REQ将被调用

,并且只有在从所有API中响应后才调用一个订户。 (全部45个API)

demoCall().subscribe(res=>{
  // this subscribe only once after all calls are completed and got success result
}) 

I have array of requests like:

let array = [req1,req2,req3.....,req(n)]

Currently I am using forkJoin to call a subscriber only after getting all requests response. Now I want to modify my code to make api calls in batch but call subscriber only after complete all batches response get is it possible?

example

public demoCalls() {
       let demoForkJoin
       var a = [req1,req2,....,req45]
       a.map((data,index)=>{
            demoFrokJoin[index] = this.http.post()
       })
       forkJoin(demoForkJoin)

}

Now instead of this, I want to call 10-10 req in batch. Each 10 req will be call after 1000ms

And one subscriber will be called only after getting response from all API's. (all 45 api's)

demoCall().subscribe(res=>{
  // this subscribe only once after all calls are completed and got success result
}) 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安穩 2025-02-18 21:53:55

我建议使用一些RXJS运算符的并发值,而不是批处理。 Mergemap 例如提供此值。完成一个可观察到的可观察后,下一个可观察的可订阅。为完成操作添加最终确定

下面的示例具有2个并发请求,

import { Observable, of, range } from 'rxjs';
import { delay, finalize, mergeMap } from 'rxjs/operators';

function req(id: number): Observable<number> {
  console.log('request received for id', id);

  return of(id).pipe(delay(5000));
}

range(0, 10)
  .pipe(
    mergeMap((r) => req(r), 2),
    finalize(() => console.log('all requests done'))
  )
  .subscribe((id) => console.log('request completed for id', id));

请参见stackblitz上的此操作:

Instead of batching I recommend to use the concurrent value of some rxjs operators. mergeMapfor example provides this value. After completing one observable, the next observable becomes subscribed to. Add finalizefor the finish action.

Following example has 2 concurrent requests

import { Observable, of, range } from 'rxjs';
import { delay, finalize, mergeMap } from 'rxjs/operators';

function req(id: number): Observable<number> {
  console.log('request received for id', id);

  return of(id).pipe(delay(5000));
}

range(0, 10)
  .pipe(
    mergeMap((r) => req(r), 2),
    finalize(() => console.log('all requests done'))
  )
  .subscribe((id) => console.log('request completed for id', id));

See this in action on Stackblitz: https://stackblitz.com/edit/typescript-cmylca?file=index.ts

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文