javascript - Function return a list of values that could only be get asynchronously - Stack Overflow

admin2025-04-09  1

I'd like to write this function:

function getResults(nums){
    var results = [];
    for(var i = 0, len = nums.length; i < len; i++){
        var num = nums[i];
        ajaxGet("/" + num, function(data){
            results.push(data);
        });
    }
    return results;
}    
var results = getResults([12, 22, 34]);

as you can see, because ajaxGet is asynchronous, this won't work. How could I do this properly?

I'd like to write this function:

function getResults(nums){
    var results = [];
    for(var i = 0, len = nums.length; i < len; i++){
        var num = nums[i];
        ajaxGet("http://xxx./" + num, function(data){
            results.push(data);
        });
    }
    return results;
}    
var results = getResults([12, 22, 34]);

as you can see, because ajaxGet is asynchronous, this won't work. How could I do this properly?

Share Improve this question asked Jan 22, 2012 at 5:09 wong2wong2 35.8k51 gold badges137 silver badges182 bronze badges 4
  • The only problem I see in your code is: results.push[data]; -> results.push(data); – Itay Moav -Malimovka Commented Jan 22, 2012 at 5:12
  • @ItayMoav Oh that's a typo, thanks. do you think the code above could get the correct result? – wong2 Commented Jan 22, 2012 at 5:14
  • @ItayMoav: Looking at porn could also be a problem. – qwertymk Commented Jan 22, 2012 at 5:24
  • Looking at Porn is never a problem – Itay Moav -Malimovka Commented Jan 22, 2012 at 5:26
Add a ment  | 

4 Answers 4

Reset to default 4

You could also tell Ajax to run synchronously

$.ajax({async: false});
var results = getResults();
$.ajax({async: true});

You need to return the array using a callback, and call the callback when you receive the last response. (note that responses will not be received in order)

function getResults(nums, callback) {
    var results = [];
    for(var i = 0, len = nums.length; i < len; i++){
        var num = nums[i];
        ajaxGet("http://xxx./" + num, function(data){
            results.push(data);
            if (results.length === nums.length)
                callback(results);
        });
    }
}    

If you're using jQuery you can set the async flag to false:

function getResults(nums){
    var results = [];
    for(var i = 0, len = nums.length; i < len; i++){
        var num = nums[i];
        jQuery.ajax({
            url: "http://xxx./" + num}, 
            async: flase,
            success: function(data){
                results.push(data);
            }
        });
    }
    return results;
}    
var results = getResults([12, 22, 34]);

But if you're doing it this way then your approach is wrong. Go with SLaks answer instead.

jQuery.ajax() API page

var allresults;

function getResults(nums){
    var results = [];
    for(var i = 0, len = nums.length; i < len; i++){
        var num = nums[i];
        ajaxGet("http://xxx./" + num, function(data){
            results.push(data);
            if(results.length == nums.length){
                 resume(results);
            }
        });
    }

}

getResults([12, 22, 34]);

function resume(results){
   allresults =  results;
   ....
   ....
}     

Like others said, use if you are not using jQuery

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744203624a235918.html

最新回复(0)