Handling disposal in async code
Problem
Your async function is awaiting a bus event when the bus gets disposed mid-flight. Without explicit handling, wait() rejects with BusDisposedError and the unhandled rejection crashes the caller.
Solution
Use BusDisposedError for instanceof checks instead of string matching:
ts
import { BusDisposedError } from '@vielzeug/eventit';
async function waitForLogin(bus: Bus<AppEvents>) {
try {
const { userId } = await bus.wait('user:login', AbortSignal.timeout(10_000));
return userId;
} catch (err) {
if (err instanceof BusDisposedError) return null; // bus torn down — graceful exit
throw err; // timeout or unexpected error — propagate
}
}Pitfalls
- Checking
err.message === 'Bus is disposed'instead oferr instanceof BusDisposedErrorbreaks when the message changes or the class is minified. Always useinstanceof. - Catching
BusDisposedErrorwithout re-throwing other errors silently swallows unexpected failures. Only catch the specific class and let everything else propagate. - A disposed bus silently drops all
emit()calls. Code that expects post-disposal emissions to arrive will hang indefinitely.