New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

wormise

Package Overview
Dependencies
Maintainers
0
Versions
50
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

wormise

With this library, you can create a Promise that executes in a new thread.

latest
Source
npmnpm
Version
2.0.45
Version published
Weekly downloads
5
-16.67%
Maintainers
0
Weekly downloads
 
Created
Source

Wormise

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

Codesandbox example link

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();

Keywords

promise

FAQs

Package last updated on 21 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts