wormise v2.0.45
Wormise
RU
Эта Node.js библиотека позволяет выполнять функцию в новом потоке и получать доступ к результатам вычислений через Promise
.
Благодаря wormise
вы можете получить удобный интерфейс-обертку для работы с вычислениями в новом потоке.
Описание
wormise(executedFunction, dir, params): Promise
dir
- папка в которой выполняется вызов wormise
executedFunction
- функция, выполняемая в отдельном потоке.
params
- параметры для executedFunction
Поддерживает ESM (wormise/esm
) и CJS (wormise/cjs
).
EN
This Node.js library allows you to execute a function in a new thread and access the results of the calculation through Promise
.
With wormise
, you can get a convenient wrapper interface to work with computations in a new thread.
Supports ESM (wormise/esm
) and CJS (wormise/cjs
).
Description
wormise(executedFunction, dir, params): Promise
dir
- the folder where the wormise call is made.
executedFunction
- function executed in a separate thread.
params
- arguments for executedFunction
Usage example
Without imports
import wormise, { wormiseDafaultDirname } from 'wormise/esm';
const dir = wormiseDafaultDirname(import.meta.url);
async function getCalculationsResult() {
try {
const result = await wormise(
params => {
// Complicated calculations
return new Date(params);
},
dir,
Date.now(),
);
console.log(result);
} catch (error) {
console.error(error);
}
}
getCalculationsResult();
With imports
import wormise, { wormiseDafaultDirname } from 'wormise/esm';
const dir = wormiseDafaultDirname(import.meta.url);
import { threadId } from 'worker_threads';
console.log({ threadId });
const data = wormise(
async () => {
const logWormiseThreadId = async () => {
const { threadId } = await import('worker_threads');
console.log({ threadId });
};
await logWormiseThreadId();
},
dir,
undefined,
);
// Output:
// { threadId: 0 }
// { threadId: 1 }
Example tsconfig.json
{
"compilerOptions": {
"target": "ESNext",
"module": "NodeNext",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitAny": true,
"skipLibCheck": true,
"moduleResolution": "NodeNext",
"noEmitHelpers": true,
"outDir": "dist"
}
}
Using in CommonJS
Just import from wormise/cjs like this:
import wormise, { wormiseDafaultDirname } from 'wormise/cjs';
async function getCalculationsResult() {
try {
const result = await wormise(
params => {
// Complicated calculations
return new Date(params);
},
__dirname,
Date.now(),
);
console.log(result);
} catch (error) {
console.error(error);
}
}
getCalculationsResult();
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago