Workers - Web documentation | Deno DocsSkip to main content

Workers

Run script operations in background threads. Manage worker threads, communicate with workers, and handle data transfer between workers and the main thread.

Eg Worker

Interfaces

I
AbstractWorkerEventMap
No documentation available
I
v
Worker

The Worker interface represents a background task that can be created via the new Worker() constructor. Workers run in a separate thread, allowing for parallel execution without blocking the main thread.

I
WorkerEventMap
No documentation available
I
WorkerOptions
No documentation available


interface Worker

extends EventTarget

The Worker interface represents a background task that can be created via the new Worker() constructor. Workers run in a separate thread, allowing for parallel execution without blocking the main thread.

Workers can be used to:

  • Perform CPU-intensive calculations
  • Process large datasets
  • Handle tasks in parallel with the main execution thread
  • Run code in isolation with its own event loop

Examples #

#
// Creating a basic worker (main.ts)
const worker = new Worker(new URL("./worker.ts", import.meta.url).href, {
  type: "module"
});

// Send data to the worker
worker.postMessage({ command: "start", data: [1, 2, 3, 4] });

// Receive messages from the worker
worker.onmessage = (e) => {
  console.log("Result from worker:", e.data);
  worker.terminate(); // Stop the worker when done
};

// Handle worker errors
worker.onerror = (e) => {
  console.error("Worker error:", e.message);
};
#
// Worker file (worker.ts)
// Worker context: self refers to the worker's global scope
self.onmessage = (e) => {
  if (e.data.command === "start") {
    // Perform calculation with the data
    const result = e.data.data.reduce((sum, num) => sum + num, 0);
    // Send result back to main thread
    self.postMessage(result);
  }
};

Properties #

#onerror: ((
this: Worker,
) => any) | null

Event handler for error events. Fired when an error occurs in the worker's execution context.

#onmessage: (() => any) | null

Event handler for message events. Fired when the worker sends data back to the main thread.

#onmessageerror: (() => any) | null

Event handler for message error events. Fired when a message cannot be deserialized.

Methods #

#postMessage(
message: any,
transfer: Transferable[],
): void

Sends a message to the worker, transferring ownership of the specified transferable objects.

#postMessage(
message: any,
): void

Sends a message to the worker.

#addEventListener<K extends keyof WorkerEventMap>(
type: K,
listener: (
this: Worker,
) => any
,
options?: boolean | AddEventListenerOptions,
): void

Adds an event listener to the worker.

#addEventListener(
type: string,
options?: boolean | AddEventListenerOptions,
): void

Adds an event listener for events whose type attribute value is type.

#removeEventListener<K extends keyof WorkerEventMap>(
type: K,
listener: (
this: Worker,
) => any
,
options?: boolean | EventListenerOptions,
): void

Removes an event listener from the worker.

#removeEventListener(
type: string,
options?: boolean | EventListenerOptions,
): void

Removes an event listener from the worker.

#terminate(): void

Immediately terminates the worker. This does not offer the worker an opportunity to finish its operations; it is stopped at once.

variable Worker

The Worker constructor creates a new Worker object that executes code in a separate thread.

Workers can import ES modules when created with the type: "module" option.

Properties #

#prototype: Worker
readonly


interface WorkerOptions

unstable

Properties #

#type: "classic" | "module"
optional
#name: string
optional

Properties #

#deno: { permissions?: Deno.PermissionOptions; }
optional

Configure permissions options to change the level of access the worker will have. By default it will inherit permissions. Note that the permissions of a worker can't be extended beyond its parent's permissions reach.

  • "inherit" will use the default behavior and take the permissions of the thread the worker is created in
  • "none" will have no permissions
  • A list of routes can be provided that are relative to the file the worker is created in to limit the access of the worker (read/write permissions only)

Example:

// mod.ts
const worker = new Worker(
  new URL("deno_worker.ts", import.meta.url).href, {
    type: "module",
    deno: {
      permissions: {
        read: true,
      },
    },
  }
);

Did you find what you needed?

Privacy policy