DEV Community

Titan
Titan

Posted on

How to Manage Threads in JavaScript / TypeScript / NodeJS

node-threads

a Simple Module to Create Threads

Commonjs Example
Modernjs Example
TypeScript Example

CommonJS Example

At First Get the important files from https://github.com/Titan3301/node-threads

const { createContinuousThreads } = require('./commonjs.cjs');
const { createThreads } = require('./commonjs.cjs');
const { createThread } = require('./commonjs.cjs');
//Create Thread
createThread(() => {
    setTimeout(function() {
        console.log(`new thread`);
    } , 5000)
})
createThreads(5 , () => {
    conole.log('multiple threads');
})
createContinuousThreads(6 , () => {
    console.log(`6 threads will run at a time :D after they end new thread will be created`);
})
Enter fullscreen mode Exit fullscreen mode

ModernJS Example

import { createContinuousThreads } from './modernjs.mjs'
import { createThreads } from './modernjs.mjs'
import { createThread } from './modernjs.mjs'
//Create Thread
createThread(() => {
    setTimeout(function() {
        console.log(`new thread`);
    } , 5000)
})
createThreads(5 , () => {
    conole.log('multiple threads');
})
createContinuousThreads(6 , () => {
    console.log(`6 threads will run at a time :D after they end new thread will be created`);
})
Enter fullscreen mode Exit fullscreen mode

TypeScript Example

import { createContinuousThreads } from '../typescript.ts';
import { createThreads } from '../typescript.ts';
import { createThread } from '../typescript.ts';
//Create Thread
createThread(() => {
    setTimeout(function() {
        console.log(`new thread`);
    } , 5000)
});
createThreads(5 , () => {
    conole.log('multiple threads');
});
createContinuousThreads(6 , () => {
    console.log(`6 threads will run at a time :D after they end new thread will be created`);
});
Enter fullscreen mode Exit fullscreen mode

Top comments (0)