The defer function allows you to run an async function while registering cleanup functions that will execute after it completes. This is useful when you need to clean up resources regardless of whether the main function succeeds or fails, similar to a finally block.
The function passed to defer receives a register function as its argument. Use this to register cleanup work that should run after the main function finishes.
By default, if a cleanup function throws an error after the main function has thrown an error, the cleanup error will be ignored. You can customize this behavior by passing { rethrow: true } to the register function to have it rethrow cleanup errors instead.