1

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.
*/
Brad Parks
  • 66,836
  • 64
  • 257
  • 336
  • You are passing `fibrous.run` a callback, with your console.log in it. Callbacks are ALWAYS going to be executed later in the call stack. You can not change that. You'll have to move your other two console.log functions around. – Brandon Sep 09 '13 at 14:11
  • my intent is to use fibrous to make async code sync. So for example, the above would actually be in a "dbObject.getData()" call, that I'd like to be able to use to just do DB operations in functional sequence, like normal. I'll clarify a bit more! – Brad Parks Sep 09 '13 at 14:20
  • 1
    @BradParks Fibrous doesn't "*make it sync*." It manages the `function`'s execution itself, pausing and continuing it at various points. This allows the coding style within that function to appear synchronous, but it still executes asynchronously and has no affect on surrounding code. – Jonathan Lonowski Sep 09 '13 at 14:25
  • Thanks for the feedback! I get that.... But I'm sure others wonder the same question I'm asking. Is the answer to run the (newly added) "runTest" routine using a fibrous.run call itself? – Brad Parks Sep 09 '13 at 14:29
  • Try using waitFor. https://github.com/luciotato/waitfor – Josh C. Sep 09 '13 at 14:30
  • waitFor looks interesting.... I'll try it tonight! – Brad Parks Sep 09 '13 at 14:38

2 Answers2

3

Is the answer to run the (newly added) "runTest" routine using a fibrous.run call itself?

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():

var test = fibrous(function () {
    var data = fs.sync.readFile('/etc/passwd');
    console.log('2');
});

And should be called with .sync():

test.sync();

var fibrous = require('fibrous');
var fs = require('fs');

var test = fibrous(function () {
  var data = fs.sync.readFile('/etc/passwd');
  console.log('2');
});

function runTest() {
  console.log('1');
  test.sync();
  console.log('3');
}

fibrous.run(runTest);
Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
0

Other a Express using :

 var express = require('express');
 var router = express.Router();
 var fibrous = require('fibrous');

 router.use(fibrous.middleware);

 router.get('/sync', function(req, res, next) {

   var order_categories = Order_Category.sync.list(options);
   console.log("Order_Category count : " , order_categories.length);

   var content_tags = Content_Tag.sync.list(options);
   console.log("content_tags count : " , content_tags.length);

   var creatives = Creative.sync.list(options);
   console.log("creatives count : " , creatives.length);

   return res.send( {
      order_categories: order_categories,
      content_tags: content_tags,
      creatives: creatives
      }
   );

 });
Murat
  • 41
  • 5