exec like this:
1 2 3 4 5 6 7 8 9 10 11
This is the js result compiled by coffee:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
It took me some minutes to understand that callback functions like the one that exec takes aren’t really executed synchronously, they are more like fire and forget, what really happens with this code is that the
exec is called and the flow continues, arriving at
console.log before the callback has actually finished and updated the values. I am sure that most of the js programmers are looking at me with their ‘noob’ faces but as I said, I am testing and learning as I go!
The code below illustrates it very well:
1 2 3 4 5 6 7 8 9 10 11 12
And this is the result of running this code. As you can see, the code reaches the Out of the function line and THEN finishes waiting for the callback to finish.
1 2 3 4
console.log line is moved inside the callback function body, it works, but again it may end printing on a totally different time than what you intended. There are talks and a feature request for Node to support a synchronous version of exec and relatives, but if this is going to happen it will be on version 0.7 or later.