DEV Community

varvolta
varvolta

Posted on • Edited on

2 1

Worker threads made easy

Create and execute tasks in real cpu threads other than the main one. (ES6 imports for now) Threadman doesn't use any dependencies. It's based on workers.

Installation

npm i threadman
Enter fullscreen mode Exit fullscreen mode

Syntax

new Thread(fn, args, options).run(callback)
// Or
new Thread(fn, args, options).run().then(callback)
Enter fullscreen mode Exit fullscreen mode

Basic usage

import { Thread }     from 'threadman'

let number = 10

const fn = (number) => number + 20
const callback = (result) => number = result
const args = [number]

new Thread(fn, args).run(callback)

// Or a callback function can be passed as third argument like below
new Thread(fn, args).run().then(callback)

Enter fullscreen mode Exit fullscreen mode

After you get the result you can access main scope again and reassign variables.

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more