Skip to main content
Version: 24.38.0

WebWorker.evaluate() 方法

🌐 WebWorker.evaluate() method

worker 中评估给定的函数。

🌐 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

要传递给 func 的参数。

🌐 Arguments to pass into func.

返回:

Promise<Awaited<ReturnType<Func>>>

func 的结果。

🌐 The result of func.

附注

🌐 Remarks

如果给定的函数返回一个 promise,evaluate 将等待该 promise 解决。

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

通常情况下,如果给定函数的返回值比 JSON 对象更复杂(例如大多数类),那么 evaluate 很可能会返回某个截断的值(或 {})。这是因为我们不是返回实际的返回值,而是通过协议将返回值传输到 Puppeteer 后反序列化得到的版本。

🌐 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.

一般来说,如果 evaluate 无法正确序列化返回值,或者你需要对返回对象使用可变的 handle,你应该使用 evaluateHandle

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