Skip to content

alvarolorentedev/web-threads

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

logomakr_09u4zz

Build Status codecov npm GitHub license Maintenance

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:

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