Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Async a function with WebWorkers
function HeavyWork(func, param, cb){
if (window.Worker){
var strFunc = 'onmessage = function(event) {' +
' postMessage((' + func.toString() + ')(event.data));' +
'}';
var blob = new Blob([strFunc], {type: 'application/javascript'});
var work = new Worker(URL.createObjectURL(blob))
work.onmessage = function(e) {
cb(e.data);
};
work.postMessage(param);
}
}
HeavyWork(function(a){
console.log(a);
return a + 1;
}, 1, function(b){
console.log(b);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.