swrv icon indicating copy to clipboard operation
swrv copied to clipboard

Omitting fetcher function argument is ambiguous?

Open tmladek opened this issue 4 years ago • 6 comments

The README states:

Note that fetcher can be any asynchronous function, so you can use your favorite data-fetching library to handle that part. If ommitted, swrv uses the Fetch api.

But later also:

To only retrieve a swrv cache response without revalidating, you can omit the fetcher function from the useSWRV call.

So, which one is it? :) Or am I misunderstanding what omitting means in the first case?

tmladek avatar Mar 21 '21 17:03 tmladek

yes it is ambiguous, thanks for noting! We need to document and maybe add some tests so it's clear. It should be:

- To only retrieve a swrv cache response without revalidating, you can omit the fetcher function from the useSWRV call
+ To only retrieve a swrv cache response without revalidating, you can set the fetcher function to `null` from the useSWRV call 

If it is "omitted" (aka undefined) then the Fetch api will be used. https://github.com/Kong/swrv/blob/bd6c7e6de2b8c2e9595b02fe1b010ac1a197cd37/src/use-swrv.ts#L189

darrenjennings avatar Mar 22 '21 03:03 darrenjennings

Relevant swr issue: https://github.com/vercel/swr/issues/738

darrenjennings avatar Mar 22 '21 03:03 darrenjennings

This is great, but the typings for it do not allow null, it should be a quick fix to just add null to the type definition.

tochoromero avatar Aug 03 '21 18:08 tochoromero

My PR resolves typing problem. https://github.com/Kong/swrv/pull/274

LumaKernel avatar Mar 10 '22 07:03 LumaKernel

Current doc is not ambiguous for me, function calls f(), f(undefined) and f(undefined, undefined) have the same arguments (geek note: except argument length). In JS, omitting argument always means supplying undefined.

Yeah, but I think it's good to clarify the doc.

LumaKernel avatar Mar 10 '22 07:03 LumaKernel

afaik, the null fetcher behaves like Promise.race([]) or () => new Promise<never>(() => {}) (meaning resolves after infinite minutes), but isVaildating value differs.

LumaKernel avatar Mar 10 '22 07:03 LumaKernel