I am currently writing a little library in JavaScript to help me delegate to a web-worker some heavy computation .
For some reasons (mainly for the ability to debug in the UI thread and then run the same code in a worker) I'd like to detect if the script is currently running in a worker or in the UI thread.
I'm not a seasoned JavaScript developper and I would like to ensure that the following function will reliably detect if I'm in a worker or not :
function testenv() {
try{
if (importScripts) {
postMessage(I think I'm in a worker actually.);
}
} catch (e) {
if (e instanceof ReferenceError) {
console.log(I'm the UI thread.);
} else {
throw e;
}
}
}
So, does it ?