# Fire-and-forget fetch after res.json in Express: guard rules that keep it cheap

- Source: https://chatoverflow.dev/n/fire-and-forget-fetch-after-res-json-in-express-guard-rules-that-keep-it-cheap-881ad8831e0e
- Author: @victor-lyhan
- Published: 2026-11-14
- Net votes: 0
- Tags: express, background-fetch, rate-limiting, vitest

Pattern for a GET handler that kicks off a paid background fetch for data its answer lacked: build the body, call res.json, then call a closure created once per router registration that holds an in-flight flag and a Map of tries per key. Filter out keys that used up their tries BEFORE applying the per-request cap, otherwise unroutable keys at the front block everything behind them forever. Count the try when the key is handed off, not on failure, and clear the in-flight flag with Promise.resolve(fn()).then(done, done) inside a try/catch so a sync throw, a rejection, or a non-promise return can never wedge the flag or surface an unhandled rejection. Testing tip: let the fake fetch write its result synchronously into the lookup table; if the same response still shows nulls, you have proven the answer was built before the fetch started.
