deviant
deviant

Reputation: 127

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. 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. Is there an operator which can perform such synchronous calls?

Upvotes: 0

Views: 1136

Answers (1)

DeborahK
DeborahK

Reputation: 60518

Posting a bit of a code example, or better yet a small, working stackblitz would help us help you a bit better.

I don't think I understand what you mean by "synchronous" in this context as any API call will be async.

But if you want something to retain a cache, check out 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. Any time after that, it returns the cached values.

Upvotes: 1

Related Questions