← Effect fixes your TypeScript

Effect fixes your TypeScript · Part 6

Your timeout doesn't cancel anything

· by Tony Tang

Promise.race lets the losing request keep running, holding sockets and writing to the database. Effect.timeout interrupts the work, the fetch really aborts, cleanup runs, and the timeout becomes a typed error.

Comment below: Ever been bitten by a zombie request?

Effect fixes your TypeScript: a series on the bugs you already ship, and how Effect makes them impossible. All code is real Effect v4 and was compiled before it went on screen. https://effect.website

Transcript

Your timeout doesn't cancel anything. Promise.race the request against a 5-second timer. The timer wins, you throw timeout, but the request is still running, still holding a socket, still writing to the database. To really stop it, you need an AbortController threaded through every function by hand. Everyone forgets one. In Effect, getUser.pipe(Effect.timeout("5 seconds")). When time's up, the work is interrupted. Effect.tryPromise hands you an AbortSignal, so the fetch really stops. Cleanup runs, and the timeout shows up in the type as a new error you have to handle. Best practice, a timeout that doesn't cancel is just a lie with a deadline. Quick question, where does getUser get its database from? Part seven, ever been bitten by a zombie request?

Full lesson in the Learn Effect course ↗ Bounding time

More from this series