Friday, December 28th, 2007
Er.js: Erlang-in-JavaScript
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:
result = yield Er.Ajax.get("http://beatniksf.com/erjs/index.html"); 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:
function myGet(url) { var pid = Er.Ajax.spawn(Er.pid(), url); yield Er.receive({ From: pid, Success: _, _:_ }, function(msg) { return msg; }); }
You can see the test page to watch it in action.












eh? requires js 1.7??
…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.
Thanks! It’s really good idea.
Multilanguage is very friendly thing ;-)