module.evaluate([options])


  • options <Object>

    • timeout <integer> 指定终止执行前要评估的毫秒数。如果执行中断,则会抛出 Error。此值必须是严格的正整数。

      ¥timeout <integer> Specifies the number of milliseconds to evaluate before terminating execution. If execution is interrupted, an Error will be thrown. This value must be a strictly positive integer.

    • breakOnSigint <boolean> 如果是 true,接收 SIGINTCtrl+C)将终止执行并抛出 Error。已通过 process.on('SIGINT') 附加的事件的现有句柄在脚本执行期间被禁用,但在此之后继续工作。默认值:false

      ¥breakOnSigint <boolean> If true, receiving SIGINT (Ctrl+C) will terminate execution and throw an Error. Existing handlers for the event that have been attached via process.on('SIGINT') are disabled during script execution, but continue to work after that. Default: false.

  • 返回:<Promise> 成功时将使用 undefined 履行。

    ¥Returns: <Promise> Fulfills with undefined upon 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.SourceTextModuleevaluate() 返回的 Promise 可以同步或异步执行:

¥For a vm.SourceTextModule, the promise returned by evaluate() may be fulfilled either synchronously or asynchronously:

  1. 如果 vm.SourceTextModule 本身或其任何依赖项中没有顶层 await,则在模块及其所有依赖项评估完毕后,该 Promise 将被同步执行。

    ¥If the vm.SourceTextModule has no top-level await in itself or any of its dependencies, the promise will be fulfilled synchronously after the module and all its dependencies have been evaluated.

    1. 如果评估成功,则 Promise 将被同步解析为 undefined

      ¥If the evaluation succeeds, the promise will be synchronously resolved to undefined.

    2. 如果评估结果抛出异常,则 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.

  2. 如果 vm.SourceTextModule 本身或其任何依赖项中存在顶层 await,则在模块及其所有依赖项评估完毕后,该 Promise 将被异步执行。

    ¥If the vm.SourceTextModule has top-level await in itself or any of its dependencies, the promise will be fulfilled asynchronously after the module and all its dependencies have been evaluated.

    1. 如果评估成功,则 Promise 将被异步解析为 undefined

      ¥If the evaluation succeeds, the promise will be asynchronously resolved to undefined.

    2. 如果评估结果抛出异常,则 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:

  1. 如果传递给其构造函数的 evaluateCallback 同步抛出异常,则 evaluate() 返回的 Promise 将被同步拒绝并抛出该异常。

    ¥If the evaluateCallback passed to its constructor throws an exception synchronously, evaluate() returns a promise that will be synchronously rejected with that exception.

  2. 如果 evaluateCallback 没有抛出异常,evaluate() 返回的 Promise 将被同步解析为 undefined

    ¥If the evaluateCallback does not throw an exception, evaluate() returns a promise that will be synchronously resolved to undefined.

vm.SyntheticModuleevaluateCallbackevaluate() 调用中同步执行,其返回值会被丢弃。这意味着如果 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:

  1. 如果初始评估以成功结束(module.status 等于 'evaluated'),则不执行任何操作,并返回一个解析为 undefined 的 Promise。

    ¥If the initial evaluation ended in success (module.status is 'evaluated'), it will do nothing and return a promise that resolves to undefined.

  2. 如果初始评估导致异常(module.status'errored'),它将重新拒绝初始评估导致的异常。

    ¥If the initial evaluation resulted in an exception (module.status is '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').