How is
@trpcio `httpBatchLink` able to fold multiple procedure calls into ONE batched call?
Like how does `Promise.all([a.query(), b.query()])` end up in only a single HTTP request
It's a pure timing trick built with a simple dataloader:
Every call you make in the same tick gets pushed into one shared buffer. Only the very first call schedules a `setTimeout(dispatch)`
Because `setTimeout` is a macrotask, it only runs after your synchronous code finishes. So by the time `dispatch` fires, all the calls from that tick are sitting in the buffer together
`dispatch` then joins them into one request (`a,b,c`), sends it, and resolves each call's individual promise from the response array
The neat part: `Promise.all` isn't special. Anything called in the same tick batches. The only thing that breaks it is an `await` in between, which yields to the event loop and flushes early
I think this is a really elegant design: using the language's semantics for batching instead of exposing an explicit API