October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

How to Wait for a Function to Return True in Puppeteer

Await page.waitForFunction() to keep evaluating a page-context predicate until it returns a truthy value. Learn its argument order, polling options, timeouts, cancellation, and when a locator is simpler.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use and await page.waitForFunction() with a predicate that evaluates in the browser page. Puppeteer keeps evaluating it until it returns a truthy value, then resolves the wait:

await page.waitForFunction(() => window.appReady === true);

The result need not be the Boolean true: any truthy value satisfies the wait. The method returns a promise, so use await when subsequent Node.js work must wait for the condition.

As an Amazon Associate I earn from qualifying purchases.

Pass options and predicate arguments in the right order

The method’s arguments are the predicate, an options object, then any values to pass to the predicate. The predicate runs in the page context, so Node.js variables are not automatically available inside it.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const selector = '.foo';

await page.waitForFunction(
  selector => Boolean(document.querySelector(selector)),
  {}, // options
  selector, // argument passed into the page-context function
);

This waits until an element matching .foo exists in the DOM. Passing the selector explicitly avoids relying on a variable from the Node.js scope.

The predicate may also be asynchronous. For example, it can perform a page-context fetch and await its response. Because Puppeteer reevaluates the predicate while waiting, make sure repeating its work is appropriate for your condition.

Set a timeout, polling mode, or cancellation

The documented default timeout is 30,000 milliseconds. You can set a different limit in the options object, disable the timeout with timeout: 0, or change the default timeout for a page with Page.setDefaultTimeout(). An AbortSignal can cancel a wait. See the Puppeteer wait options for the installed version.

await page.waitForFunction(
  () => window.appReady === true,
  { timeout: 10_000, polling: 100 },
);

The documented polling choices are:

  • 'raf' (the default) reevaluates through requestAnimationFrame; Puppeteer describes it as the tightest mode and suitable for observing styling changes.
  • 'mutation' reevaluates on DOM mutations, making it a fit when page changes to the DOM can make the condition true.
  • A number sets the polling interval in milliseconds, useful for conditions that need a fixed cadence rather than DOM-mutation or animation-frame polling.

Choose based on what can change the result. A DOM-dependent condition may suit mutation polling; a visual or style-driven change may suit raf. For a non-DOM condition, use an interval that makes sense for the application rather than polling more often than needed.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose a locator for element readiness

If the goal is to select or interact with an element, Puppeteer’s guide recommends locators. Locators automatically wait for element state. Use waitForFunction when the condition is an arbitrary page-side predicate that is not better expressed as a locator—for example, a custom application flag or a combination of page values.

The page interactions guide demonstrates a function-based locator that waits until at least three paragraphs exist and returns their text. For API details, consult the Page.waitForFunction documentation. The cited API reference identifies Page as version 25.12.0; the options reference identifies version 25.3.0, so verify the documentation matching your installed Puppeteer version for version-specific details.

Troubleshoot waits that do not resolve

  • The predicate never becomes truthy: Check that the condition matches the page’s actual state and that its dependencies are available in the page context. If it depends on a Node.js value, pass that value after the options argument.
  • The wait times out: The documented default is 30 seconds. Confirm the condition can become true, then set a suitable timeout or adjust the page’s default timeout. A timeout of zero disables the wait’s timeout, so use it only when an indefinite wait is intended.
  • The predicate uses a selector but cannot see a Node.js variable: Pass the selector as an argument after the options object, as in the example above.
  • The condition changes without the expected polling: Choose polling based on the trigger: DOM mutations, animation frames for visual changes, or a numeric interval for other conditions.
  • You need to stop waiting early: Provide an AbortSignal in the wait options and abort it from the caller when appropriate.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup:

If your task is to capture a page rather than coordinate a custom Puppeteer workflow, ScreenshotNeo returns a screenshot or PDF from one GET request. For example, this cURL call saves a WebP capture:

Best Value
The SQL Programming Language: .
  • Used Book in Good Condition
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo API documentation for request options. It removes cookie banners, newsletter popups, and chat widgets before capture; bot checks, blank pages, failed loads, and cache hits are not billed. Its MCP server lets AI agents take screenshots, and the free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000. Sign up for the free plan.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.