Re: [nodejs] Parallel asynchronous functions made funky!

4 views
Skip to first unread message

AJ ONeal

unread,
Oct 15, 2010, 11:56:33 AM10/15/10
to nod...@googlegroups.com, futures-j...@googlegroups.com
I promise I'll only spam every single async library that pops up only once per feature (excepting ludicrous outbursts from time to time).
But I also promise that I'll probably spam every single library that pops up that has a similar feature-set to futures.


I like your idea for your particular use case, I think I may steal it and add it as a "shortcut" to mine - more on that below. 

However, this is how you do a very similar pattern with FuturesJS.

  var Futures = require('futures'),
         fs = require('fs'),
         p1 = Futures.promise(), // one promise per parallel event
         p2 = Futures.promise(),
         join; // one join to handle all parallel events

   fs.readFile("test/foo.txt", 'utf-8', p1.fulfill);
   p1.when(function (err, data) {
       console.log("this gets called as soon as foo.txt has been read");
   });

   fs.readFile("test/bar.txt", 'utf-8', p2.fulfill);
   p2.when(function (err, data) {
       console.log("this gets called as soon as bar.txt has been read");
   });

   join = Futures.join(p1, p2);
   join.when(function (args1, args2) {
     assert.equal(arguments.length, 2);
     assert.equal(args1[1], 'foo\n'); // the data param of the first object
     assert.equal(args2[1], 'bar\n');      
   });
   funk.parallel(function (args1, args2) {
   });


As, I said above, you've got a neat implementation.

One thing that I really like about your approach, which I've thought of but haven't added yet, is that you create your join object once and then use an 'add' to add your promises/events one-by-one rather than all-at-once.

I've put on my list of things-that-will-get-done-in-a-month-or-so to change the API to have the option of being like yours in that respect - I think something like this:

var join = Futures.join(); // allow zero promises to start with

var fulfill = join.add(asapCallback) // return the fulfill of the promise, add the `when`

join.whenAll(function (args1, args2) {
    var err = args1[0] || args2[0]; // err was the first argument of both
    if (err) { throw new Error("youch!"); }
    // do stuff with the data portion
});

It would be very easy for me to write an extra function or two to wrap the code above into more succinct code, like yours.

There are some disadvantages to this approach when mixing synchronous and asynchronous callbacks - namely that whenAll could potentially fire more than once - with my current underpinnings.

AJ ONeal


On Fri, Oct 15, 2010 at 6:14 AM, Pau <mas...@gmail.com> wrote:
I just released funk. A tiny (almost non-existant) module to work with
parallel async functions.

I know that there already other modules to do that, but they didn't
fit my needs.
I wanted something that doesn't change the way you code and so simple
that its almost invisible.

http://github.com/masylum/funk

Example:

   var funk = require('./../lib/funk')(),
         fs = require('fs');

   funk.set('results', []);

   fs.readFile("test/foo.txt", 'utf-8', funk.add(function (er, data)
{
     this.results.push(data);
   }));

   fs.readFile("test/bar.txt", 'utf-8', funk.add(function (er, data)
{
     this.results.push(data);
   }));

   funk.parallel(function () {
     assert.equal(this.results.length, 2);
     assert.includes(this.results, 'foo\n');
     assert.includes(this.results, 'bar\n');
   });

--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.


Reply all
Reply to author
Forward
0 new messages