Skip to main content
Version: 25.3.0

Realm.evaluate() 方法

🌐 Realm.evaluate() method

在字段的上下文中评估一个函数并返回结果值。

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

如果传递给 realm.evaluate 的函数返回一个 Promise,该方法将等待该 Promise 解决并返回其值。

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

JSHandle 实例可以作为参数传递给该函数。

语法

🌐 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

要传递给 pageFunction 的参数。

🌐 Arguments to be passed to the pageFunction.

返回:

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"