Skip to main content

ES Module

Piscina supports ES modules (ESM) out of the box. This example demonstrates how to use Piscina with ES modules in both the main script and the worker file.

index.mjs
import { Piscina } from "piscina";

const piscina = new Piscina({
filename: new URL("./worker.mjs", import.meta.url).href,
});

(async () => {
const result = await piscina.run({ a: 4, b: 6 });
console.log(result);
})(); // Prints 10

The worker file is a simple ES module that exports a default function:

worker.mjs
export default ({ a, b }) => {
return a + b;
};

You can also check out this example on github.