Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // the helper
- function fan_in(target_count, final_callback)
- {
- var values = [];
- var count = 0;
- return function(cur_value) {
- values.push(cur_value);
- if (++count == target_count)
- final_callback(values);
- };
- }
- // a function that does some work asyncly
- function work_func(url, callback)
- $.ajax(url, {success: callback});
- }
- // what you want to do with all the results
- function final_func(values)
- {
- console.log("all the things:");
- console.log(values.join("\n"));
- }
- // how to use it
- var fan_in_callback = fan_in(8, final_func);
- for (var i = 0; i < 8; i++)
- work_func("http://stuff/" + i.toString(), fan_in_callback);
Advertisement
Add Comment
Please, Sign In to add comment