rakslice

fan_in

Oct 7th, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // the helper
  2. function fan_in(target_count, final_callback)
  3. {
  4.     var values = [];
  5.     var count = 0;
  6.     return function(cur_value) {
  7.         values.push(cur_value);
  8.         if (++count == target_count)
  9.             final_callback(values);
  10.     };
  11. }
  12.  
  13. // a function that does some work asyncly
  14. function work_func(url, callback)
  15.     $.ajax(url, {success: callback});
  16. }
  17.  
  18. // what you want to do with all the results
  19. function final_func(values)
  20. {
  21.     console.log("all the things:");
  22.     console.log(values.join("\n"));
  23. }
  24.  
  25. // how to use it
  26. var fan_in_callback = fan_in(8, final_func);
  27. for (var i = 0; i < 8; i++)
  28.     work_func("http://stuff/" + i.toString(), fan_in_callback);
Advertisement
Add Comment
Please, Sign In to add comment