Nsx coding - xkp/Doc GitHub Wiki

##Overview

Asynchronous javascript code is fun until, well, you write more than 20 lines of code. Consider the following straightforward algorithm: Read a file, modify it, then save it. What usually takes exactly 3 lines of code now looks like:

fs.readFile(file, function(data, error)
{
    modifyFile(data, function(newData, error)
    {
        fs.WriteFile(file, newData, function(error)
        {
            //we're done, do next task 
        });   
    })
});

And the only issue here is not only the code is 3 times larger, there are serious flow issues too: You cannot catch errors with tradiotional try/catch statements since the inner functions run on a different execution context.

Were we to try and do this process for an array of files; we would have to write a complex recursive algorithm when otherwise a simple for would do. That is the price of having a real fast asynchronous server, well... no more.

Excess coding

Our magic compiler allows you to write unobstructed synchronous code and then it converts it to asynch javascript. So, you write easy code while still enjoying all the node.js goodness. The same code will look like this in xs:

for(var file in files)
{
    try
    {
        var data = fs.readFile(file);
        var newData = modifyData(data);
        fs.writeFile(file, data);
    }
    catch()
    {
    }
}

And your server will still fly.