Giter Site home page Giter Site logo

js-async-to-sync's Introduction

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

js-async-to-sync's People

Contributors

bkase avatar es92 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

mnvx

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.