Skip to content

Instantly share code, notes, and snippets.

View roboncode's full-sized avatar

Rob Taylor roboncode

View GitHub Profile
@roboncode
roboncode / webworker.js
Created December 30, 2022 17:38
Dynamic web worker
function createWorker(fn) {
// Create a Blob object that contains the code for the worker
// create wrapper function that calls the function passed to createWorker
// and then posts the result back to the main thread
const fnStr = fn.toString()
// wrap the function in a function that calls the function and posts the result
const wrapper = `async function(evt) {
const fn = ${fnStr}