

Offer HN: Three useful functions for async javascript - EGreg

Javascript has always given you a nice, event-driven, single-threaded environment to write code, and now with Node.js, it's become even more useful.<p>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.<p>That's what I wanted to offer HN. These are the functions that I use all the time in Javascript applications.<p>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)<p><pre><code>  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'));
</code></pre>
and the Q.getter function takes a regular getter and enhances it with queue support, throttling, and batching.<p>All you have to do is do:<p><pre><code>  Users.get = Q.getter(Users.get);
</code></pre>
and now you can use it from anywhere like this:<p><pre><code>  Users.get(29381, function (user) {
     // do something with the user
  });
</code></pre>
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.<p>https://gist.github.com/778822 &#60;-- here it is<p>Anyway, check it out. I think it will help you with your javascript coding
Greg
======
mcotton
Thanks for sharing. These might be useful to contribute to underscore.js (or
similar project)

