Skip to main content

Web Threads

logomakr_09u4zz Build Status Coverage Status npm GitHub license Maintenance Donate

generic threads using web workers for the web

Installation

add it to your project using npm install web-threads --save or yarn add web-threads

Usage

Simple function with arguments

import { execute } from 'web-threads'

let func = (value) => {
return value * value
}
let params = {
fn: func.toString(),
args: [2]
}
execute(params)
.then(console.log)
.catch(console.error)

Function with context

import { execute } from 'web-threads'

function Func(value){
this.value = value
}
Func.prototype.foo = function(){
return this.value * this.value
};
var instance = new Func(2)
let params = {
fn: instance.foo,
context: instance
}
execute(params)
.then(console.log)
.catch(console.error)

Function with context and arguments

import { execute } from 'web-threads'

function Func(value){
this.value = value
}
Func.prototype.foo = function(otherValue){
return this.value * otherValue
};
var instance = new Func(2)
let params = {
fn: instance.foo,
context: instance,
args: [4]
}
execute(params)
.then(console.log)
.catch(console.error)

ES6 class call function with arguments

import { execute } from 'web-threads'

class someClass {
constructor(val){
this.val = val
}
foo(some){
return this.val * some
}
}

var instance = new someClass(2)
let params = {
fn: instance.foo,
context: instance,
args: [4]
}
execute(params)
.then(console.log)
.catch(console.error)

Inspired in:

Github Repo

Web graphic by picol from Flaticon is licensed under CC BY 3.0. Check out the new logo that I created on LogoMakr.com https://logomakr.com/09u4Zz