module.evaluate([options])
-
options<Object>-
timeout<integer> 指定终止执行前要评估的毫秒数。如果执行中断,则会抛出Error。此值必须是严格的正整数。¥
timeout<integer> Specifies the number of milliseconds to evaluate before terminating execution. If execution is interrupted, anErrorwill be thrown. This value must be a strictly positive integer. -
breakOnSigint<boolean> 如果是true,接收SIGINT(Ctrl+C)将终止执行并抛出Error。已通过process.on('SIGINT')附加的事件的现有句柄在脚本执行期间被禁用,但在此之后继续工作。默认值:false。¥
breakOnSigint<boolean> Iftrue, receivingSIGINT(Ctrl+C) will terminate execution and throw anError. Existing handlers for the event that have been attached viaprocess.on('SIGINT')are disabled during script execution, but continue to work after that. Default:false.
-
-
返回:<Promise> 成功时将使用
undefined履行。¥Returns: <Promise> Fulfills with
undefinedupon success.
评估模块及其依赖项。对应 ECMAScript 规范中 循环模块记录 的 Evaluate() 具体方法 字段。
¥Evaluate the module and its depenendencies. Corresponds to the Evaluate() concrete method field of Cyclic Module Records in the ECMAScript specification.
如果模块是 vm.SourceTextModule,则必须在模块实例化之后调用 evaluate();否则,evaluate() 将返回一个被拒绝的 Promise。
¥If the module is a vm.SourceTextModule, evaluate() must be called after the module has been instantiated;
otherwise evaluate() will return a rejected promise.
对于 vm.SourceTextModule,evaluate() 返回的 Promise 可以同步或异步执行:
¥For a vm.SourceTextModule, the promise returned by evaluate() may be fulfilled either
synchronously or asynchronously:
-
如果
vm.SourceTextModule本身或其任何依赖项中没有顶层await,则在模块及其所有依赖项评估完毕后,该 Promise 将被同步执行。¥If the
vm.SourceTextModulehas no top-levelawaitin itself or any of its dependencies, the promise will be fulfilled synchronously after the module and all its dependencies have been evaluated.-
如果评估成功,则 Promise 将被同步解析为
undefined。¥If the evaluation succeeds, the promise will be synchronously resolved to
undefined. -
如果评估结果抛出异常,则 Promise 将被同步拒绝,并抛出导致评估失败的异常,该异常与
module.error相同。¥If the evaluation results in an exception, the promise will be synchronously rejected with the exception that causes the evaluation to fail, which is the same as
module.error.
-
-
如果
vm.SourceTextModule本身或其任何依赖项中存在顶层await,则在模块及其所有依赖项评估完毕后,该 Promise 将被异步执行。¥If the
vm.SourceTextModulehas top-levelawaitin itself or any of its dependencies, the promise will be fulfilled asynchronously after the module and all its dependencies have been evaluated.-
如果评估成功,则 Promise 将被异步解析为
undefined。¥If the evaluation succeeds, the promise will be asynchronously resolved to
undefined. -
如果评估结果抛出异常,则 Promise 将被异步拒绝,并抛出导致评估失败的异常。
¥If the evaluation results in an exception, the promise will be asynchronously rejected with the exception that causes the evaluation to fail.
-
如果模块是 vm.SyntheticModule,则 evaluate() 始终返回一个同步完成的 Promise,请参阅 合成模块记录的 Evaluate() 的规范:
¥If the module is a vm.SyntheticModule, evaluate() always returns a promise that fulfills synchronously, see
the specification of Evaluate() of a Synthetic Module Record:
-
如果传递给其构造函数的
evaluateCallback同步抛出异常,则evaluate()返回的 Promise 将被同步拒绝并抛出该异常。¥If the
evaluateCallbackpassed to its constructor throws an exception synchronously,evaluate()returns a promise that will be synchronously rejected with that exception. -
如果
evaluateCallback没有抛出异常,evaluate()返回的 Promise 将被同步解析为undefined。¥If the
evaluateCallbackdoes not throw an exception,evaluate()returns a promise that will be synchronously resolved toundefined.
vm.SyntheticModule 的 evaluateCallback 在 evaluate() 调用中同步执行,其返回值会被丢弃。这意味着如果 evaluateCallback 是一个异步函数,则 evaluate() 返回的 Promise 将不会反映其异步行为,并且来自异步 evaluateCallback 的任何拒绝都将丢失。
¥The evaluateCallback of a vm.SyntheticModule is executed synchronously within the evaluate() call, and its
return value is discarded. This means if evaluateCallback is an asynchronous function, the promise returned by
evaluate() will not reflect its asynchronous behavior, and any rejections from an asynchronous
evaluateCallback will be lost.
即使模块已被执行完毕,evaluate() 也可以再次被调用,在这种情况下:
¥evaluate() could also be called again after the module has already been evaluated, in which case:
-
如果初始评估以成功结束(
module.status等于'evaluated'),则不执行任何操作,并返回一个解析为undefined的 Promise。¥If the initial evaluation ended in success (
module.statusis'evaluated'), it will do nothing and return a promise that resolves toundefined. -
如果初始评估导致异常(
module.status为'errored'),它将重新拒绝初始评估导致的异常。¥If the initial evaluation resulted in an exception (
module.statusis'errored'), it will re-reject the exception that the initial evaluation resulted in.
在评估模块时无法调用此方法(module.status 为 'evaluating')。
¥This method cannot be called while the module is being evaluated (module.status is 'evaluating').