Polyomino: a sort-of work queue for node.js
The problem with asynchronous programming is you keep firing off callback functions, but sometimes you need to wait until a bunch of them are done before you can continue. Polyomino lets you run as many asynchonous functions as you want, and then when they're all finished it runs your success function.
The constructor takes two callback functions that take a single data argument. Set up by:
polyomino = require('./path/to/polyomino'); var poly = new polyomino.polyomino(on_success, on_fail);
An instance of Polyomino has these public methos:
Adds a new callback function to the list of callbacks to get run.
Call this within a callback function you passed to add_task when the task has been completed successfully. Pass along any data you want the on_success function to have access to, and the name which is uses to access it.
Like success, but call this when your callback function has completely with an error. You can pass along error information in the data field.
Once you have added all of your callback functions with add_task, this will run them all. When they've all called either success or fail, your on_success or on_fail function will get executed.
Look at example.js to see the example code. It opens a file and grabs the data, and downloads the latest twitter data in json format, and once both are complete displays the results to you. Run it by doing: