Created
September 11, 2015 02:58
-
-
Save manrueda/a08a1b911dff0a90aa26 to your computer and use it in GitHub Desktop.
Async a function with WebWorkers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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