redline
LowAsynctimeout-zero

setTimeout(…, 0) timing hack

setTimeout(…, 0) timing hack. setTimeout(…, 0) defers work to dodge an ordering problem.

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

Flagged
setTimeout(() => input.focus(), 0);
Better
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.

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"
    }
  }
}

Read the source or report a false positive.