Run your code easily in web workers
- :zero: Zero Dependencies
- :v: Promise based architecture
- :cloud: Light Weight 
NPM -
``shell`
npm i web-workers
or
CDN -
`js`
`js
const lunar = () => {
async function get () {
const res = await fetch('https://jsonplaceholder.typicode.com/todos/1')
return res.json()
}
}
const lunarMission = WebWorker(lunar)
lunarMission.get().then(d => console.log(d))
`
The WebWorker constructor is used invoke/spawn a web worker with the script you pass.
And will return promises for all underlying methods of the script.
`js
const WebWorker = require('web-workers')
const script = function () {
console.log('Web Worker is Invocked')
function add(a, b) {
return a + b
}
}
const worker = WebWorker(script)
`
#### Params
- script - script to run in the worker thread
The call() or [method] can be used to call underlying functions/methods which are also promise based
`js
worker.add(1, 2) // 3
worker.call('add', 2, 3) // 5
`
#### Params
- ...args - arguments to pass to the method
The method is used to add a message to the worker :warning: but not post/send it.
The same message you can use and append later in the stage and post.
`js
worker.message('Hello')
worker.message({
data: [],
errors: []
})
`
To clear the message use message with no params :wink:
#### Params
- message - message to set - can be of any type - string, object, array
This method is used to append the passed message to the earlier set message
`js
worker.message('Hello')
worker.append(' World!')
`
#### Params
- message - message to append - can be of any type - string, object, array but should be matching the already set message
getMessage() returns the current message with worker
`js
worker.message('Hello')
worker.getMessage() // 'Hello'
`
kill() will terminate your worker thread
:warning: The kill method will kill the worker even if there is some execution happening
`js`
worker.kill()
onerror is an error event handler, i.e. if any error happens at the worker thread, you can listen to it right here
`js`
worker.onerror((err) => {
console.log(err)
})
Use this method to access the message posted in the worker thread, using postMessage()
`js``
worker.ondata((data) => {
console.log(data)
})