Hi Francois,
Thanks for your intro. :)
On 15 June 2012 12:42, Francois Marier <
fran...@mozilla.com> wrote:
> So far I like Node, but I'd love to discuss best practices, patterns and
> things like that because it's pretty easy for a callback-based program
> to become pretty hairy and hard to read. [snip]
On that note, thought I'd just share with you a program I wrote a week
or so back. I started it as an example of how to synchronised files in
an S3 Bucket (download only) with your local directory, but in fact it
turned into a great example of a program which uses a number of queues
to shuffle "pieces of work" around where each piece of work is
created for each object in your S3 Bucket.
*
https://github.com/appsattic/node-awssum-scripts/blob/master/bin/amazon-s3-sync-down.js
Just to describe how it works in a sentence, it sets up a number of
queues (using async) and each queue is processed by a function which
either completes the work required for that item, or pushes it to a
different queue for the next bit of work require. Take a look at the
code and it'll become clear. I just think it's a nice example of how
you can keep your callbacks and levels of indentation low. :) Oh, and
it can run all of these things concurrently by passing a number in as
an argument to the script (e.g. three concurrent uploads :
amazon-s3-sync-down.js -c 3).
The opposite 'upload' program can also be seen here, but this isn't as
nice since it was written first and needs a slight refactor:
*
https://github.com/appsattic/node-awssum-scripts/blob/master/bin/amazon-s3-sync-up.js
So yeah, the upshot out of all of that for me is to use a flow-control
library when you can. Note: not everyone's cup of tea. :)
Cheers,
Andy
--
contact: Andrew Chilton
website:
http://www.chilts.org/blog/