Concept: Never assume "2 seconds is enough." Computers vary in speed. Network latency varies. Hardcoded sleeps are the #1 cause of flaky debugging and CI failures.
The Anti-Pattern
# ❌ BAD: Guessing
submit_form()
time.sleep(2) # "Should be done by now..."
assert_success()
The Pattern: Polling with Timeout
Instead of waiting for time, wait for truth.
Structure
# ✅ GOOD: Knowing
submit_form()
start = time.time()
while time.time() - start < 10: # Maximum Timeout (Safety Valve)
if check_if_success_message_is_visible():
break # Success! Exit immediately (Fast)
time.sleep(0.1) # Tiny polling interval
else:
raise TimeoutError("Form did not submit within 10 seconds")
assert_success()
Why: If it loads in 0.1s, the test finishes in 0.1s. If it takes 4s, the test passes.
2. Async Backend Jobs
Don't use: "Wait 5 seconds for the queue to process."
Use:
# Poll the database for the status change
wait_for_condition(
lambda: db.get_order(id).status == 'PROCESSED',
timeout=10
)
3. API Consistency (Eventual Consistency)
Scenario: You create a user, but GET /users doesn't show them yet.
Fix:
def get_user_safely(user_id):
# Retry 3 times with exponential backoff
for i in range(3):
try:
return api.get_user(user_id)
except NotFound:
sleep(2 ** i) # 1s, 2s, 4s
raise NotFound
Summary
Sleeps are for humans.
Conditions are for machines.
Always define: "What specifically am I waiting to happen?" and wait for that.
1---2name: condition-based-waiting3description: condition based waiting4---5# Technique: Condition-Based Waiting67**Concept:** Never assume "2 seconds is enough." Computers vary in speed. Network latency varies. Hardcoded sleeps are the #1 cause of flaky debugging and CI failures.89## The Anti-Pattern10```python11# ❌ BAD: Guessing12submit_form()13time.sleep(2) # "Should be done by now..."14assert_success()15```1617## The Pattern: Polling with Timeout18Instead of waiting for *time*, wait for *truth*.1920### Structure21```python22# ✅ GOOD: Knowing23submit_form()2425start = time.time()26while time.time() - start < 10: # Maximum Timeout (Safety Valve)27 if check_if_success_message_is_visible():28 break # Success! Exit immediately (Fast)29 time.sleep(0.1) # Tiny polling interval30else:31 raise TimeoutError("Form did not submit within 10 seconds")3233assert_success()34```3536## Implementation Strategies3738### 1. UI Testing (Selenium/Cypress)39* **Don't use:** `sleep(5000)`40* **Use:** `await waitFor(() => expect(element).toBeVisible())`41* **Why:** If it loads in 0.1s, the test finishes in 0.1s. If it takes 4s, the test passes.4243### 2. Async Backend Jobs44* **Don't use:** "Wait 5 seconds for the queue to process."45* **Use:**46 ```python47 # Poll the database for the status change48 wait_for_condition(49 lambda: db.get_order(id).status == 'PROCESSED',50 timeout=1051 )52 ```5354### 3. API Consistency (Eventual Consistency)55* **Scenario:** You create a user, but `GET /users` doesn't show them yet.56* **Fix:**57 ```python58 def get_user_safely(user_id):59 # Retry 3 times with exponential backoff60 for i in range(3):61 try:62 return api.get_user(user_id)63 except NotFound:64 sleep(2 ** i) # 1s, 2s, 4s65 raise NotFound66 ```6768## Summary69* **Sleeps** are for humans.70* **Conditions** are for machines.71* Always define: "What specifically am I waiting to happen?" and wait for *that*.
Run npx skillmds@latest add harshahosur81/condition-based-waiting in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
condition based waiting It is listed under Coding & Dev Tools on SkillMD.
This skill has not completed SkillMD's automated safety review yet. Independent scanners report: SkillSpector: PASS, Skill Scanner: PASS. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
harshahosur81 (@harshahosur81) published this skill. Their other Agent Skills are listed on their SkillMD profile.