Question

So I've been hearing a LOT about node.js lately. The proponents seem to tout it as second coming of Christ. This piqued my interest and I decided to read up on it.

I came across multiple sources with benchmark numbers, and it clearly does an excellent job for handling large number of concurrent requests. But the 'callback' way of writing code just feels very 'unnatural' to me. A little bit of research led me to Eventlet, which lets me do asynchronous operations while writing code that looks very much like synchronous (hence making me more productive).

But Node.js seems to be much more powerful than Eventlet (http://codescience.wordpress.com/2011/09/05/non-blocking-io-node-js-and-pythons-eventlet). I'm assuming it's because of the V8 JS engine. Is there a Java/Javascript offering that allows me to perform asynchronous operations while writing synchronous code, thus giving me the best of both worlds - developer productivity along with the performance of JVM/V8?

Was it helpful?

Solution

Node.js supports can support async - await syntax allowing you to run asynchronous operations with synchronous looking code

Check this out:

https://github.com/koush/node/wiki/%22async%22-support-in-node.js

OTHER TIPS

There are a few projects aiming to bring this kind of style to Node.js. IcedCoffeeScript, a fork of CoffeeScript, is one example (but requires you to write CoffeeScript). Another is node-fibers.

Personally, I've been experimenting with IcedCoffeeScript (as I already use CoffeeScript) and it seems to work pretty well (see my before-and-after example), but debugging CoffeeScript is a bit of a pain, although the next version CoffeeScript should have source mapping support that will hopefully make this easier.

You should check out one of my projects, it is called sharpnr.js. This is also using the await-async syntax, but it's a simple JavaScript library, which works in every browser.

The library is currently in beta, but it's stable and supports almost every statement (for example loops, switch, if), and works well with existing libraries (like jQuery).

The syntax is almost identical to the C# version:

var getAsync = async(function(url) {
  var result = await; $.get(url);
  $("#test").html(result);
});
getAsync("http://www.sharpnrjs.com");

Working example on jsfiddle.

You can download the library from github.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top