Run the following function inside of a Worker (ie another thread).
@paraminputs — Values to clone across to the worker and pass into the function.@paramworkerFunction — Sync or async function to run on the worker side, which receivesinputsas its first argument.@returnsA Promise which resolves toworkerFunction(inputs)(evaluated in the worker thread).
NOTE:
workerFunctiongets turned into a source code string which gets evaluated in the other thread. Therefore, it can't access any closure variables. To pass data into the function, use theinputsparameter.
Example:
const someName = "bob";
// By passing `someName` as a property of `inputs` and destructuring it from
// the first parameter to `workerFunction`, we can reference it with the same
// name it uses outside of `workerFunction`. It's not a true closure, but it
// feels like one.
const uppercased = runInWorker({ someName }, ({ someName }) => {
return someName.toUpperCase();
});
console.log(uppercased); // "BOB"If you need to access imports inside the worker, use require.
NOTE: Because
inputsand the return value ofworkerFunctionmust cross the thread boundary, the data is cloned and re-created on the other side. Not all types can be cloned. The types which can be cloned are:string,number,boolean,null,undefined, Arrays, Objects,Date,RegExp,ArrayBuffer, Typed arrays,DataView, and instances of native Error constructors (Error,TypeError, etc).SharedArrayBuffer can also be used to shared live data across the worker and main thread.
declare function runInWorker<
Inputs extends import("quickjs:os").StructuredClonable,
Output extends import("quickjs:os").StructuredClonable,
>(
inputs: Inputs,
workerFunction: (inputs: Inputs) => Promise<Output>,
): Promise<Output>;