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:
- 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.- If the evaluation succeeds, the promise will be synchronously resolved to
undefined. - 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.
- If the evaluation succeeds, the promise will be synchronously resolved to
- 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.- If the evaluation succeeds, the promise will be asynchronously resolved to
undefined. - If the evaluation results in an exception, the promise will be asynchronously rejected with the exception that causes the evaluation to fail.
- If the evaluation succeeds, the promise will be asynchronously resolved to
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:
- If the
evaluateCallbackpassed to its constructor throws an exception synchronously,evaluate()returns a promise that will be synchronously rejected with that exception. - If the
evaluateCallbackdoes not throw an exception,evaluate()returns a promise that will be synchronously resolved toundefined.
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:
- If the initial evaluation ended in success (
module.statusis'evaluated'), it will do nothing and return a promise that resolves toundefined. - If the initial evaluation resulted in an exception (
module.statusis'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').