Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Here's one thing that never made sense to me: lets say a request comes in and data needs to be collected from 3 different sources, A, B, and C before a response can be given back to the client. You say that it would take max(A, B, C) in order to retrieve all the data. Consider this pretty standard snippet:

    function get(source, callback) {
        var result = getData(source);
        callback(result);
    }

    get(A, function(result) {
        get(B, function(result) {
            get(C, function(result) {
                // CREATE RESPONSE WITH A, B, AND C HERE
            });
        });
    });
Wouldn't you have to wait for the data to be retrieved before running the next callback resulting in a time of A + B + C anyways? Am I missing something about the way to retrieve data from multiple sources? I don't see how max(A, B, C) is possible while still knowing for sure when all the data has been collected.


This method has basically turned an async request into a synchronous request. One idea is that you make requests A, B, and C concurrently, parse the data, then have a handler which waits for the 3 to complete and then combines them into a single response, as opposed to cascading all the callbacks.


I understand that, but I feel like a majority of the javascript async code I've seen has all been in this format. By chance, do you have an example of handler which would wait for multiple async requests to complete?


So I have yet to find a project to use node on, my case is mostly for Async AJAX requests.

Here's a very simple version (which I admit to still using occasionally)

  RequestHandler = {
      numberOfRequests: 3
      makeRequests: function() {
          var __this = this;
          a(function() {
              // Parse Data
              __this.completeRequest();
          })
          b(function() {
              // Parse Data
              __this.completeRequest();
          })
          c(function() {
              // Parse Data
              __this.completeRequest();
          })
      },
      completeRequest: function() {
         this.numberOfRequests--;
         if(this.numberOfRequests == 0 ) this.doDone();
      }
      doDone: function() {
        // We now have all data, do what we will.
      } 
  }
  RequestHandler.makeRequests();


You could write your own way of doing it or use an existing flow control library. There are lots. :)

https://github.com/joyent/node/wiki/modules#wiki-async-flow

For a specific example, look at async.parallel: https://github.com/caolan/async


I wish I knew about this kind of thing last time I hacked on node.js... I would have had such better & cleaner code. This clears up a lot. Thanks


There are two distinct issues here : (a) throughput (b) concurrency -- how many concurrent incoming requests you can handle. Event I/O based frameworks help you with maximizing concurrency.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: