← ClaudeAtlas

condition-based-waitinglisted

Use when waiting for something async. NEVER use Start-Sleep blindly — wait on conditions.
liujiarui0918/claude-code-codex-strongest · ★ 0 · AI & Automation · score 62
Install: claude install-skill liujiarui0918/claude-code-codex-strongest
# Condition-Based Waiting Time-based sleeps are a bet that the world finishes within N seconds. The bet is always wrong eventually — too short on slow machines, too long on fast ones. Wait on the **condition**, not the **clock**. ## Iron Law **Do not `Start-Sleep` to wait for something to "be ready."** Poll the actual condition, with a timeout, with backoff. ## Red Flags - `Start-Sleep 5` after starting a server, then hitting it. - `Start-Sleep 60` to "let things settle." - Sleeps inside retry loops with no condition check. - Sleeps tuned to "what worked on my machine." - Sleep duration in a comment: `# this needs to be at least 3s`. If you've written any of these, replace them now. ## The Right Pattern ``` loop: if condition_met(): break if elapsed > timeout: fail("waited too long for X") sleep(small backoff) ``` Three pieces — condition check, timeout guard, bounded sleep between polls. ## Common Conditions and How to Wait on Them ### Wait for a port to open (server starting) ```powershell $deadline = (Get-Date).AddSeconds(30) $delay = 0.2 while ((Get-Date) -lt $deadline) { try { $c = New-Object System.Net.Sockets.TcpClient $c.Connect('127.0.0.1', 8080) $c.Close() break } catch { Start-Sleep -Seconds $delay $delay = [Math]::Min($delay * 1.5, 2.0) } } if ((Get-Date) -ge $deadline) { throw "server did not open port 8080 within 30s" } ``` ### Wait for a file to exist ```powershell $deadline = (Get-