Simple Threading with JS through NodeJS Cluster
const thread = require("thread-process");
function temp() {
    console.log("Hello World ~Thread");
}
const thr = new Thread();
thr.store(temp);
thr.run();
thr.close();- Supports "require" in "threaded" functions
 - Supports storing both synchronous and asynchronous functions
 - Multiple threads running simultaneously
 - Thread emitted events
 - Thread cleanup
 
Requires prior installation of NodeJS
$ npm install thread-process
const thread = require("thread-process");Thread.MAX_THREADS // Gets the maximum CPU count
Thread.OPEN_THREADS // Gets the count of how many threads are activenew Thread(); // Constructing a thread with default settings
new Thread(function); // Stores a function(Async) returns the function stored
thr.store(function);
thr.store(function, function.name); // Override the function name or provide one that is missingTHREAD_DATA // (Object) Contains all the functions stored
FOCUSED_FUNCTION // (String) The most recent function
RETURN(data); // Sends data from the function to the main thread
Run the most recent function stored or executed by the thread
(Async) returns the data returned by the function executed
thr.run();
thr.runOnce(); // Closes the thread after runningRun target function stored in the thread
thr.run("temp"); // Runs temp()Run with arguments
thr.run(function.name, [arg1,arg2,arg3]) // temp(arg1, arg2, arg3)
thr.run([arg1,arg2,arg3])(Async) returns data as an Array
Thread.runAll(thr.run(), ...);Event handling can make life much easier!
thr.on("completed", (data)=>{}); // Contains the returned data
thr.on("returned", (data)=>{}); // Contains data returned by RETURN(data);thr.close();Runs a function within a thread and immediately closes the thread upon completion
(Async) returns the data returned by the function executed
Thread.exec(function);
Thread.exec(function, [arg1,arg2,arg3]);Using thread constiables
const temp = (text)=>{
    console.log(THREAD_DATA);
    console.log(FOCUSED_FUNCTION); // Outputs "temp"
}
async function main(){
    const tp = new Thread();
    await tp.store(temp);
    await tp.runOnce();
}
main();Asynchronous management of threads & passing values
const temp = async (text)=>{
    console.log(`Async! ${text}`);
}
async function main(){
    const tp = new Thread();
    await tp.store(temp);
    await tp.runOnce(["~ Thread"]);
}
main();Multiple stored functions
const func1 = ()=>{console.log("First function!")}
const func2 = ()=>{console.log("Second function!")}
async function main(){
    const tp = new Thread();
    await tp.store(func1);
    await tp.store(func2);
    await tp.runOnce("func1");
}
main();Using then instead of async/await
const temp = ()=>{console.log("Hello World!")}
const tp = new Thread();
tp.store(temp).then((result)=>{tp.runOnce();});Running simultaneous threads
const temp = ()=>{return "Hello";}
const tp = new Thread(temp);
const tp2 = new Thread(temp);
Thread.runAll(tp.runOnce(), tp.runOnce()).then((result)=>console.log(result));Running with events
const temp = ()=>{RETURN("Hello")};
const tp = new Thread(temp);
tp.on("returned", (data)=>{
    console.log(data);
    tp.close();
});
tp.run();