But sometimes we need to do things with async and writing the same code over and over gets cumbersome. It can drive a person nuts (http://news.ycombinator.com/item?id=2101210) ... until you decide to just grab a couple functions that help you express yourself.
That's what I wanted to offer HN. These are the functions that I use all the time in Javascript applications.
The first one is called "pipe" and it basically lets you express yourself simply by saying "wait until objects X and Y are available, and then do something)
var p = Q.pipe(['user', 'stream], function (params, subjects) {
// arguments that were passed are in params.user, params.stream
// this objects that were passed are in subjects.user, subjects.stream
mysql("SELECT * FROM subscription WHERE user.id = " + params.user.id +
" AND stream.id = " + params.stream.id, this.fill('subscription');
}, ['user', 'stream', 'subscription'], function (params, subjects) {
// now we have all three objects ready to be used
}, function () {
// execute this function every time something is piped
});
mysql("SELECT * FROM user WHERE user_id = 2", p.fill('user'));
mysql("SELECT * FROM stream WHERE publisher_id = 2", p.fill('stream'));
and the Q.getter function takes a regular getter and enhances it with queue support, throttling, and batching.All you have to do is do:
Users.get = Q.getter(Users.get);
and now you can use it from anywhere like this: Users.get(29381, function (user) {
// do something with the user
});
without having to worry about who else has requested the same user. If the user is cached, the tail call occurs right away. Otherwise, the function calls the "real" getter to fetch it. If someone else requests the same user, they are placed in a waiting queue. When the user is finally fetched, ALL the callbacks are invoked. Not only that, but you can also tell it to throttle on a resource. Just use a string or an object for the throttle. And it will make sure there are only 10 open requests at a time. And also you can implement batching.https://gist.github.com/778822 <-- here it is
Anyway, check it out. I think it will help you with your javascript coding Greg