Calling postMessage in Web Worker onmessage callback throws SYNTAX_ERR: DOM Exception 12

2.4k views Asked by At

When writing the onmessage callback for a Web Worker I get an "Uncaught Error: SYNTAX_ERR: DOM Exception 12" in my console when I try to send another postMessage().

var w = new Worker(url);
w.onmessage = function(e) {
    if(e.data.msg=='validate'){
        if(validateWork(e.data.wrk)){
            postMessage('proceed');
        }
    }
}
2

There are 2 answers

0
Jesse Hattabaugh On BEST ANSWER

You must call postMessage() using this when in a callback.

this.postMessage('proceed');
0
AudioBubble On

I think this would work too:

w.postMessage('proceed');