Er.js: Erlang-in-JavaScript

Written by on December 28th, 2007 in Ajax News.

Alex Graveley has built Er.js, a library that “piggybacks on Neil Mix’s Thread.js which fakes threading in JavaScript 1.7 using coroutines and nested generator continuations. The goal is to replicate Erlang’s concurrent lockless process model and message-passing APIs in JavaScript.”

Alex also added initial concurrent Ajax support:

XmlHttpRequest, AJAX and JSON integrate nicely with the process and message-passing model, allowing processes to avoid asynchronous JavaScript and callbacks.

Instead, using message-passing and concurrency, Er.js makes network access transparent, without blocking other processes or interactivity:

JAVASCRIPT:

  1.  
  2. result = yield Er.Ajax.get(”http://beatniksf.com/erjs/index.html”);
  3. alert(”Fetched Content: ” + result.Text);
  4.  

Under the covers, this is accomplished using a concurrently spawned process (started via Er.Ajax.spawn), which handles XmlHttpRequest internals. The spawned process uses Er.send to tell our process about download progress and completion.

Er.Ajax.get and others (post, json, etc) are implemented by yielding execution until the final message from the spawned process is received, and then returning it to the caller:

JAVASCRIPT:

  1.  
  2. function myGet(url) {
  3.    var pid = Er.Ajax.spawn(Er.pid(), url);
  4.    yield Er.receive({ From: pid, Success: _, _:_ },
  5.                     function(msg) { return msg; });
  6. }
  7.  

You can see the test page to watch it in action.

Source: Ajaxian
Original Article: http://feeds.feedburner.com/~r/ajaxian/~3/207520553/erjs-erlang-in-javascript

Comments are closed.



Site Navigation