News for hackers. Stuff that matters.
Re:Multiple Simultaneous Ajax Requests (with one callback) i... by Groxx on Friday February 14, 2014 @01:42PM (#7240170)
Or you can make a count-down latch of some kind:

  var num_results = 3;
  var results = []
  var makeCallback = function(index) {
    return function(response) {
      num_results -= 1;
      results[index] = response;
      if (num_results > 0) {
        return;
      }
      
      // actually do stuff here, all results are loaded.
    };
  };
  $.get("/a", makeCallback(0));
  $.get("/b", makeCallback(1));
  $.get("/c", makeCallback(2));
  // etc
It's also not hard to make a generic latch-producing function so you can just write stuff like:

  var l = makeLatch(callback);
  $.get("/a", l());
  $.get("/b", l());
  $.get("/c", l());
Re:Multiple Simultaneous Ajax Requests (with one callback) i... by rybosome on Friday February 14, 2014 @06:49PM (#7241904)
True, and this would work, but why would you favor this over Promises? They're an open, well-known spec with a lot of implementations - hell, some browsers support them natively. Promises also offer better control over sequential action and error handling.
Re:Multiple Simultaneous Ajax Requests (with one callback) i... by Stal3r on Friday February 14, 2014 @05:01PM (#7241293)
Yes, you can reinvent wheels and avoid javascript best practices, and lose all benefits of async code control.
Re:Multiple Simultaneous Ajax Requests (with one callback) i... by Groxx on Tuesday February 18, 2014 @02:54PM (#7260624)
Latches aren't async code control? Javascript has (async) best practices that survive more than a couple years?

Promises are great, I totally agree (as long as they're A+ compliant). But they're not on every platform, and it's not (usually) worth building your own if it's not there.

Re:Multiple Simultaneous Ajax Requests (with one callback) i... by tmarthal on Friday February 14, 2014 @05:33PM (#7241505)
A 'count-down latch' is commonly (at least when I learned it, using pthreads) called a 'Conditional Variable'.

In your case case, your mutex would be the num_results variable and the conditional is that it is greater than 0.