Answer the question
In order to leave comments, you need to log in
How to work with workers in js?
I work with web workers and faced the following problem:
worker.postMessage(someData)
console.log('text')
configureWebpack: process.env.NODE_ENV === 'development'
? {
module: {
rules: [
{
test: /\.worker\.js$/,
use: {
loader: 'worker-loader',
options: {
inline: true
}
}
}
]
}
}
: {},
self.addEventListener('message', event => {
const data = event.data;
switch (data.cmd) {
case 'getInfo':
// eslint-disable-next-line no-case-declarations
const info = fetchInfo(data.token);
self.postMessage(info);
break;
default:
throw 'Unknown command'
}
});
import Worker from './custom.worker.js'
const worker = new Worker();
worker.postMessage({
cmd: 'getInfo',
data: {
token
},
})
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question