WebView.evaluate method | Bun module | Bun
BuildDocsReferenceGuidesBlogDiscord/
Bun/
WebView/
evaluateMevaluate
Search the reference...
/
BuildDocsReferenceGuidesBlogDiscord/
Bun/
WebView/
evaluateMevaluate
method
WebView.evaluate r.json())"); // awaited
```
**`script` must be an expression.** For statement sequences, wrap in
an IIFE: `evaluate("(() => { let x = f(); return x + 1 })()")`.
Values that `JSON.stringify` collapses to `undefined` (functions,
symbols, `undefined` itself) resolve to `undefined`. Circular
references reject.
Only one `evaluate()` may be in flight at a time per view; a second
concurrent call throws `ERR_INVALID_STATE`." data-algolia-static="false" data-algolia-merged="false" data-type="Method">
evaluateT = unknown>(script: string): PromiseT>;
Run a JavaScript expression in the page's main frame and return the result as a native JS value.
The expression is wrapped as await (${script}) — if it evaluates to a Promise, the promise is awaited. The resolved value is serialized page-side via JSON.stringify and deserialized here, so arrays and objects come back as real structures:
await view.evaluate("document.title"); // string
await view.evaluate("[1, 2, 3]"); // number[]
await view.evaluate("({ a: 1, b: true })"); // { a: number, b: boolean }
await view.evaluate("fetch('/api').then(r => r.json())"); // awaited
script must be an expression. For statement sequences, wrap in an IIFE: evaluate("(() => { let x = f(); return x + 1 })()").
Values that JSON.stringify collapses to undefined (functions, symbols, undefined itself) resolve to undefined. Circular references reject.
Only one evaluate() may be in flight at a time per view; a second concurrent call throws ERR_INVALID_STATE.
Resources
ReferenceDocsGuidesDiscordMerch StoreGitHubBlog Toolkit
RuntimePackage managerTest runnerBundlerPackage runnerProject
Bun 1.0Bun 1.1Bun 1.2Bun 1.3RoadmapContributingLicenseBaked with ❤️ in San Francisco
We're hiring →