Mevaluate
Bun

method

vm.SyntheticModule.evaluate

): Promise<void>;

Evaluate the module and its depenendencies. Corresponds to the Evaluate() concrete method field of Cyclic Module Records in the ECMAScript specification.

If the module is a vm.SourceTextModule, evaluate() must be called after the module has been instantiated; otherwise evaluate() will return a rejected promise.

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

  1. 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. If the evaluation succeeds, the promise will be synchronously resolved to undefined.
    2. 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. 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. If the evaluation succeeds, the promise will be asynchronously resolved to undefined.
    2. If the evaluation results in an exception, the promise will be asynchronously rejected with the exception that causes the evaluation to fail.

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. If the evaluateCallback passed to its constructor throws an exception synchronously, evaluate() returns a promise that will be synchronously rejected with that exception.
  2. If the evaluateCallback does not throw an exception, evaluate() returns a promise that will be synchronously resolved to undefined.

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() could also be called again after the module has already been evaluated, in which case:

  1. If the initial evaluation ended in success (module.status is 'evaluated'), it will do nothing and return a promise that resolves to undefined.
  2. If the initial evaluation resulted in an exception (module.status is 'errored'), it will re-reject the exception that the initial evaluation resulted in.

This method cannot be called while the module is being evaluated (module.status is 'evaluating').

@returns

Fulfills with undefined upon success.

Referenced types

interface ModuleEvaluateOptions

  • breakOnSigint?: boolean

    If true, the execution will be terminated when SIGINT (Ctrl+C) is received. Existing handlers for the event that have been attached via process.on('SIGINT') will be disabled during script execution, but will continue to work after that. If execution is terminated, an Error will be thrown.

  • timeout?: number

    Specifies the number of milliseconds to execute code before terminating execution. If execution is terminated, an Error will be thrown. This value must be a strictly positive integer.