Friday, December 28th, 2007

Er.js: Erlang-in-JavaScript

Category: JavaScript

<p>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
< view plain text >
  1. result = yield Er.Ajax.get("http://beatniksf.com/erjs/index.html");
  2. alert("Fetched Content: " + result.Text);

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
< view plain text >
  1. function myGet(url) {
  2.    var pid = Er.Ajax.spawn(Er.pid(), url);
  3.    yield Er.receive({ From: pid, Success: _, _:_ },
  4.                     function(msg) { return msg; });
  5. }

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

Posted by Dion Almaer at 7:25 am
3 Comments

++++-
4.4 rating from 14 votes

3 Comments »

Comments feed TrackBack URI

eh? requires js 1.7??

Comment by Rizqi Ahmad — December 28, 2007

…it’s great to see this sort of innovation — where a developer spawns off an idea from one language into a different language… also great to see all of the innovative ideas flowing into optimization for client-side technologies for the web-based paradigm.

Comment by Mark Holton — December 28, 2007

Thanks! It’s really good idea.
Multilanguage is very friendly thing ;-)

Comment by Snowcore — December 29, 2007

Leave a comment

You must be logged in to post a comment.