Angular RXJS:如何对相同的API进行多次调用,并标记并组合结果

发布于 2025-02-04 09:36:54 字数 870 浏览 2 评论 0原文

我正在对同一API进行多个呼叫(具有不同的有效负载),然后将所有这些调用的结果(所有对象阵列)合并在一起。 我已经成功实现了这一目标。

但是,我需要标记每个结果数组的这些结果(每个对象),以便以后可以过滤它们,

即来自API调用1的对象,我可以添加一个属性,例如,api_id as api_id as api_id as api_id

as this this,object从API呼叫2的结果将具有API_ID:“ 2”, API_ID:“ 3”用于第三个API呼叫等等...

    public GetTriplets(args: {payload: any, limit: number}[]) {
        const urls = [];
        args.forEach(arg => 
            urls.push(this.http.post(environment.url+ `/data/docs?limit=${arg.limit}`, arg.payload))
        );
        return merge(...urls).pipe(
            reduce((result:[], response:[]) => result.concat(response)),
            mergeMap(
                (res: []) => res && res.length > 0 ?
                    of(res)
                    : throwError('There are no docsfor the selected parameters.')

            )
        );
    }

I'm making multiple calls (with different payloads) to the same API and then merging the results (all arrays of objects) of all these calls together.
I have acheived this much successfully.

However I need to mark these results (each object) of each result array so that I can filter them later,

i.e. For a object coming from API call 1, I could add a property, say api_id with value "1"

Like this, object from result of API call 2 would have api_id: "2",
api_id:"3" for 3rd API call and so on...

    public GetTriplets(args: {payload: any, limit: number}[]) {
        const urls = [];
        args.forEach(arg => 
            urls.push(this.http.post(environment.url+ `/data/docs?limit=${arg.limit}`, arg.payload))
        );
        return merge(...urls).pipe(
            reduce((result:[], response:[]) => result.concat(response)),
            mergeMap(
                (res: []) => res && res.length > 0 ?
                    of(res)
                    : throwError('There are no docsfor the selected parameters.')

            )
        );
    }

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

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

发布评论

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

评论(1

┈┾☆殇 2025-02-11 09:36:54

并行发送多个http请求,然后在一个地方访问所有结果是 forkjoin 操作员。

public GetTriplets(args: { payload: any, limit: number }[]) {
  // accumulate the HTTP requests to send
  const urls = args.map((arg) => this.http.post(environment.url + `/data/docs?limit=${arg.limit}`, arg.payload));

  // send all HTTP requests
  return forkJoin(urls).pipe(
    
    // process the array of response objects for each request, in order
    switchMap((responses) => {
      ... //  mark each response (each object) of each result array so that you can filter them later
    }),

    // if any of the requests fails, emit error message
    catchError((res) => throwError('There are no docs for the selected parameters.'))
  );

Sending multiple HTTP requests in parallel, then accessing all their results in one place is a good use-case for the forkJoin operator.

public GetTriplets(args: { payload: any, limit: number }[]) {
  // accumulate the HTTP requests to send
  const urls = args.map((arg) => this.http.post(environment.url + `/data/docs?limit=${arg.limit}`, arg.payload));

  // send all HTTP requests
  return forkJoin(urls).pipe(
    
    // process the array of response objects for each request, in order
    switchMap((responses) => {
      ... //  mark each response (each object) of each result array so that you can filter them later
    }),

    // if any of the requests fails, emit error message
    catchError((res) => throwError('There are no docs for the selected parameters.'))
  );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文