DEV Community

varvolta
varvolta

Posted on • Updated on

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)