Skip to content

Experiments in asyncToSync converters in web workers

Notifications You must be signed in to change notification settings

bkase/js-async-to-sync

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

JavaScript asyncToSync

Synchrously execute any async function by blocking in a worker

Why is this useful?

Sometime you have to work with generated code that expects a synchronous delegate, and you don't want to mess with the compiler (e.g. an Emscripten file system).

via Service Worker

What?

We've always had a synchronous function via XMLHttpRequests. This wasn't useful for making arbitrary async functions synchronous because it could only get results from a server. However, now Service Workers allow us to intercept network calls. We use this to intercept and route synchronous xhr calls from a worker, and block until the the function completes.

How do I run this?

  1. Run a static webserver and open index.html (like python -m SimpleHTTPServer)

Caveats

  • As implemented blocking calls can only be made from a Web Worker
  • Only works in Chrome and Firefox

via SharedArrayBuffer

What?

TC39 introduced SharedArrayBuffers and Atomics. Now we can pre-allocate a buffer and share the same one between the (ui thread?) and a web worker. Using Atomics' wake and wait, we can block in the worker and signal from UI. We can also communicate back to the worker using the same buffer.

How do I run this?

  1. Get latest Chrome Canary
  2. Go to chrome://flags
  3. Enable "Experimental enabled SharedArrayBuffer support in JavaScript" (search for #shared-array-buffer)
  4. Run a static webserver and open index.html (like python -m SimpleHTTPServer)

Caveats

  • Only works in Chrome Canary

About

Experiments in asyncToSync converters in web workers

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published