puppeteer/website/versioned_docs/version-21.7.0/api/puppeteer.webworker.evaluate.md
release-please[bot] 864012a86d
chore: release main (#11593)
Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
2024-01-04 13:39:46 +00:00

1.5 KiB

sidebar_label
WebWorker.evaluate

WebWorker.evaluate() method

Evaluates a given function in the worker.

Signature:

class WebWorker {
  evaluate<
    Params extends unknown[],
    Func extends EvaluateFunc<Params> = EvaluateFunc<Params>,
  >(func: Func | string, ...args: Params): Promise<Awaited<ReturnType<Func>>>;
}

Parameters

Parameter Type Description
func Func | string Function to be evaluated.
args Params Arguments to pass into func.

Returns:

Promise<Awaited<ReturnType<Func>>>

The result of func.

Remarks

If the given function returns a promise, evaluate will wait for the promise to resolve.

As a rule of thumb, if the return value of the given function is more complicated than a JSON object (e.g. most classes), then evaluate will _likely_ return some truncated value (or {}). This is because we are not returning the actual return value, but a deserialized version as a result of transferring the return value through a protocol to Puppeteer.

In general, you should use evaluateHandle if evaluate cannot serialize the return value properly or you need a mutable handle to the return object.