diff --git a/API.md b/API.md deleted file mode 100644 index 1b09273..0000000 --- a/API.md +++ /dev/null @@ -1,65 +0,0 @@ -## Modules - -
-
nodejs-threads
-
-
- -## Functions - -
-
createWorker(workerPath, [payload])
-

Function to create a new worker thread. Calling this function will instantiate a new worker thread -and return a promise which resolves to the reference to the worker thread. The worker thread will -be executed in a separate process.

-
-
defineWorker(job)
-

Function to define what should be executed inside the worker thread. -The async function will be executed as soon as the worker thread is created.

-
-
- - - -## nodejs-threads - - -## createWorker(workerPath, [payload]) ⇒ -Function to create a new worker thread. Calling this function will instantiate a new worker thread and return a promise which resolves to the reference to the worker thread. The worker thread will be executed in a separate process. - -**Kind**: global function -**Returns**: Promise which resolves to the reference of the worker -**Throws**: - -- Error If workerPath is not a string - -**Created**: 27-DEC-2021 -**Author**: Karan Raina - -| Param | Type | Description | -| --- | --- | --- | -| workerPath | string | Path to the worker file | -| [payload] | object | Payload to be sent to the worker | - -**Example** -```js -const { createWorker } = require('nodejs-threads'); // OR import { createWorker } from 'nodejs-threads'; // Inside any async function const worker = await createWorker('./worker.js', { range: 50000000, }); // Attach a listener if you expect any return value from the worker funcion worker.on('message', (result) => { console.log(result); }); -``` - - -## defineWorker(job) ⇒ -Function to define what should be executed inside the worker thread. The async function will be executed as soon as the worker thread is created. - -**Kind**: global function -**Returns**: Returns a promise which resolves to the result of the job -**Created**: 27-DEC-2021 -**Author**: Karan Raina - -| Param | Type | Description | -| --- | --- | --- | -| job | function | Async function to be executed in the worker | - -**Example** -```js -// This code will execute in a separate thread const { defineWorker } = require('nodejs-threads'); // OR import { defineWorker } from 'nodejs-threads'; defineWorker(async (payload) => { console.log(payload); // Payload from the Primary thread is availbale here // Do any CPU intensive task here. // The event loop in primary thread won't be blocked . let result = 0; for (let i = 0; i < payload.range; i++) { result += i; } console.log('COMPLETED'); return result; }); -``` diff --git a/README.md b/README.md index 39e1a70..5f02663 100644 --- a/README.md +++ b/README.md @@ -34,61 +34,104 @@ npm install nodejs-threads ## API Guide -### 🔥 Read our [API.md](https://github.com/karankraina/nodejs-threads/blob/main/API.md) file for detailed API documentation. + +## Modules -## Basic Usage +
+
nodejs-threads
+
+
-### Define a worker job that needs to be run in a separate thread. +### Functions -Use the ```defineWorker``` function to define a worker job in ```worker.js``` file (can be named anything). This function takes an async function as argument. It passes the payload received from the main thread to the argument function of the worker thread. +
+
createWorker(workerPath, [payload])
+

Function to create a new worker thread. Calling this function will instantiate a new worker thread +and return a promise which resolves to the reference to the worker thread. The worker thread will +be executed in a separate process.

+
+
defineWorker(job)
+

Function to define what should be executed inside the worker thread. +The async function will be executed as soon as the worker thread is created.

+
+
-```javascript -/** - * This code will execute in a separate thread -*/ -const { defineWorker } = require('nodejs-threads'); -// OR -import { defineWorker } from 'nodejs-threads'; + +### nodejs-threads + -defineWorker(async (payload) => { - console.log(payload); // Payload from the Primary thread is availbale here +### createWorker(workerPath, [payload]) ⇒ +Function to create a new worker thread. Calling this function will instantiate a new worker thread +and return a promise which resolves to the reference to the worker thread. The worker thread will +be executed in a separate process. - /* - * Do any CPU intensive task here. - * The event loop in primary thread won't be blocked . - */ - let result = 0; - for (let i = 0; i < payload.range; i++) { - result += i; - } - console.log('COMPLETED'); - return result; -}); -``` +**Kind**: global function +**Returns**: Promise which resolves to the reference of the worker +**Throws**: -### Create a worker thread +- Error If workerPath is not a string -You can create a new worker thread by simply calling ```createWorker``` function. The first argument is the path of the ```worker.js``` file and you can pass any payload as the second argument. The payload passed will be provided as an argument in the worker callback function. +**Created**: 27-DEC-2021 +**Author**: Karan Raina -```javascript +| Param | Type | Description | +| --- | --- | --- | +| workerPath | string | Path to the worker file | +| [payload] | object | Payload to be sent to the worker | + +**Example** +```js const { createWorker } = require('nodejs-threads'); // OR import { createWorker } from 'nodejs-threads'; - // Inside any async function const worker = await createWorker('./worker.js', { range: 50000000, }); - // Attach a listener if you expect any return value from the worker funcion worker.on('message', (result) => { console.log(result); }); +``` + + +### defineWorker(job) ⇒ +Function to define what should be executed inside the worker thread. +The async function will be executed as soon as the worker thread is created. + +**Kind**: global function +**Returns**: Returns a promise which resolves to the result of the job +**Created**: 27-DEC-2021 +**Author**: Karan Raina + +| Param | Type | Description | +| --- | --- | --- | +| job | function | Async function to be executed in the worker | + +**Example** +```js +// This code will execute in a separate thread +const { defineWorker } = require('nodejs-threads'); +// OR +import { defineWorker } from 'nodejs-threads'; +defineWorker(async (payload) => { + console.log(payload); // Payload from the Primary thread is availbale here + // Do any CPU intensive task here. + // The event loop in primary thread won't be blocked . + + let result = 0; + for (let i = 0; i < payload.range; i++) { + result += i; + } + console.log('COMPLETED'); + return result; +}); ``` + ## Run tests