Created
January 17, 2017 18:42
-
-
Save zackdouglas/68a4768c29f8928e1264b14e9db4b9c2 to your computer and use it in GitHub Desktop.
Launch a function as a Worker
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 launchWorker(f) { | |
if (! (f instanceof Function)) { | |
throw new Error(`Cannot launch ${typeof(f)} as Worker`); | |
} | |
const src = `(${f})();`; | |
const blob = new Blob([src], {type: 'application/javascript'}); | |
const url = URL.createObjectURL(blob); | |
return new Worker(url); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment