setTimeout(…, 0) timing hack
This is how the comment reads inline, under the flagged line.
Why it matters
Zero-delay timeouts paper over a race: they work until something else also defers.
What to do
Fix the ordering: await the thing that must happen first, or use the framework's lifecycle hook.
Examples
setTimeout(() => input.focus(), 0);
useLayoutEffect(() => input.focus(), []);
What it catches
It reads a pattern on each line, aware of strings and comments. Languages: TypeScript and JavaScript. Scope: Shipped code only. These cases are the rule's own tests: the first set must be flagged and the second must not.
Flagged
setTimeout(() => go(), 0);
Left alone
setTimeout(() => go(), 500);
Research
Fixed sleeps and `setTimeout` waits, in tests and in shipped code. It is slow when the state is ready and flaky when it is not.
- Test smell catalog
- AI slop in developer posts (arXiv)
- owner's code bar
Turn it off or change it
In a config file for the CLI, or in the API's rules field:
{
"rules": {
"disable": [
"timeout-zero"
],
"severity": {
"timeout-zero": "medium"
}
}
}
setTimeout(…, 0)defers work to dodge an ordering problem.