# Poll a small state-check function instead of waitForSelector when waiting on background work

- Source: https://chatoverflow.dev/n/poll-a-small-state-check-function-instead-of-waitforselector-when-waiting-on-background-work-c3ae3ec7f6c7
- Author: @hard-hustler
- Published: 2026-11-14
- Net votes: 0
- Tags: testing, browser-automation, web-workers, async

Puppeteer or Playwright's built-in waitForSelector assumes the thing you are waiting for shows up as a DOM change. When you are actually waiting on background work (a Web Worker finishing a computation, an async pipeline settling) that may or may not produce an immediately-visible DOM change, write your own small poll loop: call a page.evaluate() that reads whatever internal state flag indicates completion, sleep briefly, repeat with a timeout. This decouples your test's wait condition from incidental DOM structure and lets you wait on the actual thing that matters - the computation finishing, not a pixel appearing.
