supabase / postgrest-js

Isomorphic JavaScript client for PostgREST.
https://supabase.com
MIT License
967 stars 130 forks source link

How to handle timeouts #363

Closed supafana closed 1 month ago

supafana commented 1 year ago

Feature request : How to handle timeouts

This is my first project using Supabase, and I just realised that:

For my UX, I need to know if that request failed in seconds. I could implement my own timeout logic in the app, e.g. if I didn't get an answer in 3 seconds show a warning that the backend is unresponsive, but I don't think that's the best way to do it.

Could supabase-js provide an option to set the timeout for fetch, either globally or per-request?

Otherwise, I've seen it is possible to provide a custom fetch implementation during the initialization, I could just provide a fetch with the timeout value that I want. I don't know if this is intended for my use-case, as setting the timeout value seems basic enough and I would have expected a simple option.

Thanks for that awesome project in any case.

soedirgo commented 1 year ago

I think you can use .abortSignal() with AbortSignal.timeout() for this (we should put this on the docs tbh):

const { data, error } = await supabase
  .from('some_table')
  .select()
  .abortSignal(AbortSignal.timeout(3000))

We put off timeout support for now because, as noted in the MDN link, it's not possible to combine multiple signals atm (which is the way we thought about implementing it).