condition-based-waiting

Replace arbitrary timeouts with condition polling for state changes.

Updated Nov 3, 2025
One-click install
npx skills add https://github.com/ayourtch/kimichat --skill condition-based-waiting-ayourtch
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: condition-based-waiting
Source: https://github.com/ayourtch/kimichat/tree/main/skills/condition-based-waiting
Command: npx skills add https://github.com/ayourtch/kimichat --skill condition-based-waiting-ayourtch

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill solves flaky tests caused by arbitrary setTimeout or sleep calls, replacing them with reliable condition-based polling that waits for actual state changes.

Core Features & Use Cases

  • Reliable Async Testing: Provides a generic waitFor utility to poll for conditions (e.g., event presence, state change, count reached).
  • Flaky Test Elimination: Prevents race conditions and inconsistent test failures by removing timing guesses, making your test suite robust.
  • Efficiency: Polls every 10ms, balancing responsiveness with CPU usage, and includes timeouts to prevent infinite loops.
  • Use Case: When an async test fails intermittently because it's waiting for a TOOL_RESULT event with a fixed setTimeout, use this skill to replace it with waitForEvent(manager, threadId, 'TOOL_RESULT').

Quick Start

I'm using the condition-based-waiting skill to fix the flaky user-login.test.ts by waiting for the AUTHENTICATED event.

Frequently Asked Questions about condition-based-waiting

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I fix flaky tests caused by arbitrary timeouts?

Replace fixed `setTimeout` or `sleep` calls with condition-based polling that waits for actual state changes. This eliminates race conditions by checking for specific events or conditions instead of guessing timing, making your test suite reliable across local, CI, and distributed environments.

What's the best way to wait for async state changes in tests?

Use a generic polling loop that evaluates a condition function at regular intervals until it resolves or times out. This approach balances responsiveness with CPU efficiency, typically polling every 10ms, and returns the resolved value with informative errors when conditions aren't met.

Can I use condition-based waiting to handle race conditions in event-driven tests?

Yes. Condition-based waiting handles race conditions by polling for specific events (like `TOOL_RESULT` or `AUTHENTICATED`) or state changes rather than relying on fixed delays. It works with event managers, state listeners, and readiness checks across async workflows and IO availability scenarios.

How does polling help prevent intermittent test failures?

Polling repeatedly checks whether a condition is true instead of assuming timing will work. Since it enforces a timeout and fails with clear errors when conditions aren't met, you catch real issues rather than masking timing problems with longer sleeps.

When should I replace sleep calls with condition polling in my test suite?

Use condition polling whenever your tests wait for events, state transitions, or resource availability. Replace `setTimeout` with condition-based waiting if tests fail intermittently, depend on arbitrary delays, or involve async workflows prone to flakiness.