Skip to main content
Version: 24.41.0

Realm.evaluate() method

Evaluates a function in the realm's context and returns the resulting value.

If the function passed to realm.evaluate returns a Promise, the method will wait for the promise to resolve and return its value.

JSHandle instances can be passed as arguments to the function.

Signature

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

Parameters

Parameter

Type

Description

pageFunction

Func | string

A function to be evaluated in the realm.

args

Params

Arguments to be passed to the pageFunction.

Returns:

Promise<Awaited<ReturnType<Func>>>

A promise that resolves to the return value of the function.

Example

const result = await realm.evaluate(() => {
return Promise.resolve(8 * 7);
});
console.log(result); // prints "56"