Unable to use any other function inside the worker function even when it is in the same module. /* eslint-disable no-restricted-globals */ export function heavyCalc() { for (let i = 0; i < 1_000_000_000; i++) {} } export function workerizedFib() { self.onmessage = function (e) { console.log('Data from parent:', e.data); heavyCalc(); self.postMessage('Message from worker function'); }; } Uncaught ReferenceError: heavyCalc is not defined. Please advise.
when using Web Workers, the script is executed in a separate global scope, which means it doesn't have access to functions or variables defined in the main thread. importScripts('path/to/your/module.js'); // heavyCalc() should be there self.onmessage = function (e) { console.log('Data from parent:', e.data); heavyCalc(); self.postMessage('Message from worker function'); };
Cool, I like the your alternative implementation of ‘eval’ in JavaScript with blobs
Unable to use any other function inside the worker function even when it is in the same module.
/* eslint-disable no-restricted-globals */
export function heavyCalc() {
for (let i = 0; i < 1_000_000_000; i++) {}
}
export function workerizedFib() {
self.onmessage = function (e) {
console.log('Data from parent:', e.data);
heavyCalc();
self.postMessage('Message from worker function');
};
}
Uncaught ReferenceError: heavyCalc is not defined.
Please advise.
Me too. Did you solve it?😣
@@-n9052 nope
when using Web Workers, the script is executed in a separate global scope, which means it doesn't have access to functions or variables defined in the main thread.
importScripts('path/to/your/module.js'); // heavyCalc() should be there
self.onmessage = function (e) {
console.log('Data from parent:', e.data);
heavyCalc();
self.postMessage('Message from worker function');
};
cool 😎