chrispanag/memoized-node-fetch

Q: Comparison with react-query's fetchQuery?

fivecar opened this issue · 3 comments

First, thanks for creating this library. It seems potentially like exactly what I'd want.

Second, your README says that this handles a case that react-query doesn't, which is the unification of multiple ongoing fetches into the same promise. Is this different from what fetchQuery in react-query claims to do? Specifically:

...fetchQuery is async and will ensure that duplicate requests for this query are not created with useQuery instances for the same query are rendered while the data is fetching.

xref quote here

Or is it potentially that fetchQuery itself is the problem (i.e. it doesn't unify simultaneous outgoing requests into the same promise) even though it purports to do so for useQuery hooks?

Thanks for clarifying! Really appreciate the work.

Upon a little more digging, the core query object in react-query ~ seems ~ to return the same promise. Specifically, in query.fetch source:

  fetch(
    options?: QueryOptions<TQueryFnData, TError, TData>,
    fetchOptions?: FetchOptions
  ): Promise<TData> {
    if (this.state.isFetching)
      if (this.state.dataUpdatedAt && fetchOptions?.cancelRefetch) {
        // Silently cancel current fetch if the user wants to cancel refetches
        this.cancel({ silent: true })
      } else if (this.promise) {
        // Return current promise if we are already fetching
        return this.promise
      }
  ...

And query objects are cached in a queryMap that's used by each queryClient.

The big caveat is that I've researched this all of 5 mins, while you obviously know a lot more about this whole thing (to the point where you created a package specifically to solve this problem), so I apologize beforehand for the great likelihood that I'm misunderstanding the whole thing.

Hello @fivecar! Thanks for the interest and the nice words!

When I created this package, react-query was somewhere around version 2.19.0. Back then, after some people asked me, I checked if react-query does promise caching, and as far as I digged into it, I didn't find any evidence of promise caching.

It seems like this feature was added with v3 around 10 months ago.

Screenshot 2021-09-30 at 12 49 58

Some more facts:

  1. When I created this package, I mostly intended to use it in nodejs, and not in the browser, so I didn't even consider checking any existing packages for the browser.
  2. Well, react-query does 100,000 more things (which is generally good) but some times you don't need all of those, so if that's the case, I would choose my package.

Awesome - super-helpful, @chrispanag ! I've created #8 as a result of your thorough explanation, which I really appreciate!

Thanks again for all the time and effort you put into this. Love it!