I'm new to Node.js, and realized that one of the big differences with it and client side javascript is how asynchronous everything is.
To try and address this, I'm attempting to use fibrous to turn my code back into a more functional style of programming, but having some problems:
How can I make the following fibrous code work ?
for example, the I'd like the following code to print 1,2,3, but it prints 1,3,2
function test()
{
var fibrous = require('fibrous');
var fs = require('fs');
fibrous.run(function() {
var data = fs.sync.readFile('/etc/passwd');
console.log('2');
});
}
function runTest()
{
console.log('1');
test();
console.log('3');
}
runTest();
// This prints 1,3,2 to the console, not 1,2,3 as I'd like.
in a real use case, the above routine would be wrap a DB method that runs async, and make it so I could write things like:
var dbTable = new dbTableWrapper();
var data = dbTable.getData();
/*
... do things with the data.
The "getData" routine is the same as my "test" function above.
*/
That's part of it, yeah. Fibrous will need to call
runTest
itself to be able to manage its execution.Then,
test
just needs to be wrapped rather than.run()
:And should be called with
.sync()
: