Arc namespace provides BEAM-specific concurrency primitives that enable Erlang-style actor programming in JavaScript. These functions allow you to spawn lightweight processes, send and receive messages between processes, and leverage the full power of the BEAM VM’s concurrency model.
Arc.spawn()
Spawns a new BEAM process running the provided function. Returns aPid object representing the spawned process.
The function to execute in the new process. The function runs in complete isolation with its own heap and call stack.
Pid - A process identifier object that can be used with Arc.send() to communicate with the spawned process.
Actor pattern example
The classic actor pattern usingArc.spawn() for stateful concurrent processes:
counter_actor.js
Arc.send()
Sends a message to a BEAM process. Messages are serialized into a portable format for cross-process transfer.The process identifier to send the message to. Must be a
Pid object returned from Arc.spawn() or Arc.self().The message to send. Supports primitives, plain objects, arrays, and PIDs. Functions, promises, and special objects cannot be serialized.
TypeError if pid is not a Pid or if message contains non-serializable values.
Serialization rules
Supported types
- Primitives:
undefined,null,boolean,number,string,bigint,symbol - Plain objects and arrays
Pidobjects
Unsupported types
- Functions
- Promises
- Special objects (Date, RegExp, Map, Set)
- Circular references
- Non-enumerable properties
Arc.receive()
Blocks the current BEAM process waiting for a message. Returns the received message orundefined on timeout.
Optional timeout in milliseconds. If provided, returns
undefined when the timeout expires. Without a timeout, blocks indefinitely until a message arrives.undefined if the timeout expired.
Ping-pong example
ping_pong.js
Arc.self()
Returns aPid object representing the current BEAM process.
Returns: Pid - The process identifier for the currently executing process.
Each process has a unique
Pid that can be sent in messages, enabling reply patterns and process supervision.Arc.log()
Prints values to stdout, space-separated, with a newline. Similar toconsole.log but available in all spawned processes.
Values to print. Multiple arguments are joined with spaces. All JavaScript types are supported.
undefined
Unlike
console.log, Arc.log() is available in spawned processes where the console global may not be defined.Arc.sleep()
Suspends the current BEAM process for the specified number of milliseconds. Maps directly to Erlang’stimer:sleep/1.
The number of milliseconds to sleep. Must be a positive integer. Values less than or equal to 0 are treated as no-op.
undefined
Arc.peek()
Inspects the internal state of a Promise without affecting it. Returns an object describing the promise state.The promise object to inspect.
Promise state:
"pending", "resolved", or "rejected"Present when
type is "resolved". The fulfillment value.Present when
type is "rejected". The rejection reason.TypeError if the argument is not a Promise.
Pid object
Process identifier objects are returned byArc.spawn() and Arc.self(). They can be sent in messages and used as identifiers for process communication.
Pid.prototype.toString()
Returns a string representation of the process ID in Erlang format. Returns:string - Process ID in the format "Pid<X.Y.Z>"
The
toString() method is automatically called when converting a Pid to a string.