Usint ECMAScript modules with node.script
As of Max v8.6, in addition to scripts (using the .js extension), node.script also supports ECMAScript Modules (ESM) and associated features like top-level await. The easiest way to load an ES module is using the extension for the file you'll be passing as the argument to node.script
Please refer to the Node.JS documentation to learn more about ECMAScript modules and their interoperability with CommonJS modules.
node.script lifecycle with top-level await
With the introduction of support for using the await syntax in ECMAScript modules at the top-level
of your code the asynchronous API of the max-api module can be used in the global scope. Given that
all inter-process communication has been set up prior to loading your module all API method calls, incl.
In order to understand the implication of that on the lifecyle messages of node.script
let's assume the following code in your entrypoint module:
import { post } from "max-api"
// Simple sleep utility function to simulate an asynchronous task
const sleep = time => new Promise(resolve => setTimeout(resolve, time))
await post("Starting async task")
await sleep(1000)
await post("Finished async task")
When sending a message to the node.script object you will see the
following sequence of events:
- A message out of rightmost outlet to signal that the process has started successfully
- A message out of rightmost outlet to signal that the process is about to load the module
- node.script will post to the Max Console.
- About 1 second later node.script will post to the Max Console.
- A message out of rightmost outlet to signal that the process has successfully loaded the module
See Also
Name | Description |
---|---|
Node for Max Documentation - Table of Contents | Node for Max Documentation - Table of Contents |
node.script |