JSFiddle - React, Tailwind, and code Playground
by Jason Miller
HTML
<pre id="output"></pre>
JavaScript
/** Run an async function in its own thread. */
function worker(fn) {
let w = new Worker(URL.createObjectURL(new Blob([`
onmessage=(f => ({ data }) => Promise.resolve().then(
() => f.apply(f, data[1])
).then(
d => postMessage([data[0], null, d]),
e => postMessage([data[0], ''+e])
))(${fn})
`]))), c=0, p={}
w.onmessage = ({ data: [c,e,d] }) => {
p[c][e?1:0](e||d)
delete p[c]
}
return (...a) => new Promise( (y, n) => {
p[++c] = [y, n]
w.postMessage([c, a])
})
}
let get = worker(async url => {
let res = await fetch(url)
//throw Error('carp')
return await res.json()
})
setTimeout(async () => {
let obj = await get('https://api.github.com/users/developit')
output.textContent = JSON.stringify(obj, 0, 2);
})