waitForAsyncOptions function infrastructure ~ 66.7%

Last updated: 2026-03-04T23:21:38.391Z

Metrics

LOC: 50 Complexity: 9 Params: 3 Coverage: 66.7% (8/12 lines, 0x executed)

Signature

waitForAsyncOptions( menu: HTMLElement, timeoutMs: number, ): : Promise<void>

Summary

Waits for react-select options to appear after a search/filter. Handles both: - Local filtering (quick, 50–100ms) - Async-loaded options via API (slow, 500–2000ms) Polls for: 1. Actual .react-select__option elements to appear 2. Loading indicators (.react-select__loading-message) to disappear 3. No-options message appearing as final state Returns when options are ready OR timeout expires.

Source Code

async function waitForAsyncOptions(
  menu: HTMLElement,
  timeoutMs: number,
): Promise<void> {
  const startTime = Date.now();
  const pollIntervalMs = 100;

  const hasOptions = () =>
    menu.querySelector<HTMLElement>(
      ".react-select__option:not(.react-select__option--is-disabled)",
    ) !== null;

  const isLoading = () =>
    menu.querySelector<HTMLElement>(".react-select__loading-message") !== null;

  const hasNoOptions = () =>
    menu.querySelector<HTMLElement>(".react-select__menu-notice") !== null;

  // Quick check: are options already there?
  if (hasOptions() || hasNoOptions()) {
    log.debug("Opções já presentes no menu react-select");
    return;
  }

  // Poll until options appear, loading completes, or timeout
  while (Date.now() - startTime < timeoutMs) {
    if (hasOptions() || hasNoOptions()) {
      log.debug(
        `Opções assincronamente carregadas após ${Date.now() - startTime}ms`,
      );
      return;
    }

    if (!isLoading()) {
      // No longer loading but no options either — likely a "no results" state
      // Give it a bit more time to render the notice
      await new Promise<void>((r) => setTimeout(r, 100));
      if (hasNoOptions() || hasOptions()) {
        log.debug("Menu estabilizou (sem resultados ou com opções)");
        return;
      }
    }

    await new Promise<void>((r) => setTimeout(r, pollIntervalMs));
  }

  log.warn(
    `Timeout aguardando opções assincronamente carregadas (${timeoutMs}ms)`,
  );
}

No outgoing dependencies.

Impact (Incoming)

graph LR waitForAsyncOptions["waitForAsyncOptions"] fill["fill"] fill -->|calls| waitForAsyncOptions style waitForAsyncOptions fill:#dbeafe,stroke:#2563eb,stroke-width:2px click waitForAsyncOptions "5cd4187e2bc102f2.html" click fill "7aa5c88a65ec6164.html"
SourceType
fill calls