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 - -
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.
-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.
-Error
If workerPath is not a string
-
-**Created**: 27-DEC-2021
-**Author**: Karan Raina 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 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
+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.
+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.
+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 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 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