SupabaseS
Supabase4y ago
urth

Help with understanding why the query in JS SDK works like it works.

Hi,

EDIT: I'll make the code look as code to improve readability, didn't know there was an option

I had an issue where my API Endpoint, which was tasked with setting online status like this:

await locals.supabase.rpc('set_online', {user_id: user.id, online: true});

Was delaying the API response for a simple SET call ( I was not reading anything, nor was there a reason to wait for this call to finish ).

I checked the Networks tab, saw that the API response is 200ms. Alright, what if I do this without awaiting for the call:

locals.supabase.rpc('set_online', {user_id: user.id, online: true});

Instant improvement, 60ms call. However, no change was made inside the database. What's the fix? It is:
locals.supabase.rpc('set_online', {user_id: user.id, online: true}).then(f => f)

Response is sent back in 60ms and the value is updated, which made me wonder why I had to call then(f => f) when I do nothing with the resolved promise. As I understand it, the type which is returned from:


locals.supabase.rpc('set_online', {user_id: user.id, online: true})

Is a query builder,. To my understanding, a query builder type has to be built in an async way, and after it's done with setting up the paramaters, we actually make the call ( why I have to use await or useless then ).

Questions:

  1. Is my understanding of the supabase ways correct?
  2. If it is, why is there a need for the query builder type to be build in an async fashion? If possible, could you point me to the code which is responsible for the query builder type to be built async?
  3. Is this mentioned in the docs anywhere?
Thanks!
Was this page helpful?