简体   繁体   English

rxjs - 同步forkJoin请求

[英]rxjs - synchronous forkJoin request

I have a number of requests to perform and the are being called in forkJoin() at the same time, also they are pointing to the exact same endpoint.我有许多请求要执行,并且同时在 forkJoin() 中调用它们,它们也指向完全相同的端点。 I would like to perform forkJoin in a synchronous way, where only first request would be an API call, which would cache returned entries, and every next one will be just geting the data from the cache.我想以同步方式执行 forkJoin,其中只有第一个请求是 API 调用,它会缓存返回的条目,而下一个请求只会从缓存中获取数据。 Is there an operator which can perform such synchronous calls?是否有可以执行此类同步调用的操作员?

Posting a bit of a code example, or better yet a small , working stackblitz would help us help you a bit better.发布一些代码示例,或者更好的是,发布一个的、有效的 stackblitz 将帮助我们更好地帮助您。

I don't think I understand what you mean by "synchronous" in this context as any API call will be async.我不认为我理解您在这种情况下所说的“同步”是什么意思,因为任何 API 调用都是异步的。

But if you want something to retain a cache, check out shareReplay()但是如果你想要保留缓存的东西,请查看shareReplay()

  // All product categories
  productCategories$ = this.http.get<ProductCategory[]>(this.productCategoriesUrl)
    .pipe(
      tap(data => console.log('categories', JSON.stringify(data))),
      shareReplay(1),
      catchError(this.handleError)
    );

When the product categories are retrieved the first time, it issues the http request.第一次检索产品类别时,它会发出 http 请求。 Any time after that, it returns the cached values.此后的任何时候,它都会返回缓存的值。

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

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