fork download
  1. var Q = function(funcs, callback){
  2. var returnCount = 0;
  3. if( typeof(funcs) == "function" ) funcs = [funcs];
  4. var totalCount = funcs.length;
  5. var values = [];
  6.  
  7. var createCallback = function(i){
  8. return function(result){
  9. values[i] = result;
  10. returnCount++;
  11. if( returnCount == totalCount ){
  12. callback(values);
  13. }
  14. }
  15. }
  16.  
  17. for(var i=0;i<totalCount;i++){
  18. // How to keep the scope?
  19. funcs[i](createCallback(i));
  20. }
  21. }
  22.  
  23. function A(o){
  24. this.v = o || 10;
  25. }
  26.  
  27. A.prototype.test = function(cb){ cb(this.v); }
  28.  
  29. var a = new A();
  30. var b = new A(20);
  31.  
  32. Q([a.test, b.test], function(results){
  33. print(results[0]);
  34. print(results[1]);
  35. });
Success #stdin #stdout 0.43s 381888KB
stdin
Standard input is empty
stdout
undefined
undefined