Getting a return value back from a web worker
javascript, web-worker
Solution
The point of web workers is to delegate tasks without blocking the UI. They can't be synchronous.
You probably don't need the task to be synchronous but you need to design your code around events and asynchronous tasks just like you can't design any javascript application nor any serious GUI application with only synchronous operations.
Problem
Right now, I've got this, and it works: ``` var myWebWorker = new Worker('myWebWorker.js'); myWebWorker.onmessage = function (myEvent) { $('#Print').append('Return value: ' + myEvent.data + "<br>"); }; myWebWorker.postMessage(2); ``` My question is: Can I do this instead? ``` var result = myWebWorker.postMessage(2); ``` Because I need the web worker to be synchronous - in other words, give a return value and also don't return until you're finished. Edit 1: The web worker is doing an insert/select onto a local database using openDatabaseSync transactions. Edit 2: It appears that my problem is with Cocoa Touch and not with JavaScript. Here's a clever hack that someone has posted on StackOverflow. Edit 3: Here's a better hack.