There is a helper in the Munchable server that is 20 lines long and has its own header comment explaining why it exists. The explanation is short: there were three of it.

One lived in the background job utilities. One lived in the route that reads a photographed label. One lived in the cache that holds a shared lookup table. They were identical apart from the error each one rejected with. Nobody had decided to write three. Each author needed a timeout, remembered writing one before, and did not remember where.

If you want to see where the label route sits in the product, the front door is munchable.app and the way in to the app is munchable.app/get-started.

The helper

export class StepTimeout extends Error { constructor( public readonly step: string, public readonly ms: number, ) { super(`step "${step}" timed out after ${ms} ms`); this.name = 'StepTimeout'; } } export function withTimeout<T>(p: Promise<T>, ms: number, step: string): Promise<T> { return new Promise<T>((resolve, reject) => { const t = setTimeout(() => reject(new StepTimeout(step, ms)), ms); p.then( (v) => (clearTimeout(t), resolve(v)), (e) => (clearTimeout(t), reject(e)), ); }); } 

Two details deserve a sentence each.

The timer is cleared on both paths. The tempting version uses Promise.race([p, new Promise((_, rej) => setTimeout(rej, ms))]). That works, and then leaves a live timer behind every call that succeeded. In a serverless function that is a handle keeping the process warm for the length of the timeout, and in a long running job it is a pile of timers that all fire later and reject promises nobody is listening to. Clearing the timer in both the resolve branch and the reject branch means a finished step leaves nothing behind.

The error carries the step name. step "load-overlay" timed out after 3000 ms is a log line you can act on. Timeout is not.

Why one class mattered more than one function

The merge was not tidiness for its own sake. The comment in the file says the real cost: with three copies, a stall in a job, a stall in the label route and a stall in the cache were three differently shaped errors, "and nothing keyed on 'timed out' could see all of them."

That is the pattern worth watching for. Duplicated code is not usually the bug. The bug is that the duplicates drift apart in the place you later want to be uniform, which is error handling. Once there is a single StepTimeout, the places that catch errors can do error instanceof StepTimeout and treat a slow dependency as a different kind of problem from a wrong answer. A retry policy can retry the first and not the second. A dashboard can count them.

What it deliberately does not do

withTimeout does not cancel anything. When the timer wins, the original promise keeps running. The database query is still in flight and the model call is still being paid for. The helper only stops the caller from waiting.

That is a real limit, and the way to live with it is to be honest about which steps are safe to abandon. In the label route the deadlines are small named constants: a three second budget for database steps, an eight second budget for the one slow call, and a fifteen second window after which that call is not attempted at all. The timeout around the slow call is set to its own budget plus a database step, so the inner code has a chance to give up cleanly before the outer wrapper gives up on it. The wrapper is the backstop. The real deadline is passed down into the work.

If you need cancellation, use an AbortSignal and pass it into the call. If you only need the request to stop being stuck, a timeout that names its step is enough, and it is much easier to read in production.

Three call sites, one import

After the merge there are four call sites in three files: the label route twice, the cache once and the job runner once. They import from one file, the file has no dependencies, and the header comment records the history so the next person who thinks "I need a timeout" reads it before writing a fourth.

That header is the cheapest part of the change and probably the most valuable. A utility that says why it exists, and what it replaced, is much harder to quietly duplicate.