JavaScript jQuery run function after $.each ajax request has completed jquery function after page load,jquery function after delay,jquery

There is no need to store the result of each call inside a separate array, you have access to each response via the then method:

var requests = [];
$.each(products, function (index, product) {
    requests.push($.ajax({
        type: 'GET',
        url: 'http://localhost:1337/api/product_availability?__url_path_param=' + formatProductName(product.product_name)        
    }));
});

$.when.apply($, requests).then(function() {
    console.log(arguments.length + " results returned");
    for(var i=0;i<arguments.length;i++){
        var arg = arguments[i];
       console.log(arg);   
    }
});

Live example (using jsfiddle json echo for demo): http://jsfiddle.net/vzq4Lwm8/


Having read your comment, there is a better solution, using a combination of $.Deferred() and the complete function on an $.ajax call:

var deferreds = $.map(products, function (product) {
    var formattedProduct = formatProductName(product)
    var defer = $.Deferred();
    $.ajax({
        type: 'GET',
        url: 'http://localhost:1337/api/product_availability?__url_path_param=' + formattedProduct,
        complete: function (response) {
            if (response.status == 200) {
                defer.resolve(formattedProduct);
            }
            else{
                defer.resolve(null);
            }
        }
    })
    return defer;
});


$.when.apply($, deferreds).then(function() {
    console.log(arguments.length + " results returned");
    var availableProducts = $.map(arguments, function(x) { return x });
    // availableProducts will only contain items which returned 200 response
});

Live demo: http://jsfiddle.net/vzq4Lwm8/1/

Answer:1

My application uses iframes to display content. I need to detect whether a page is loaded in an iframe. The problem is I need to do it with php, javascript manipulation of the DOM is not enough, I ...

My application uses iframes to display content. I need to detect whether a page is loaded in an iframe. The problem is I need to do it with php, javascript manipulation of the DOM is not enough, I ...

I'm following the book Getting Started With Meteor and I'm really not getting far because simple errors keep blocking me. At this point in time I've started writing the initial app in the book in ...

I'm following the book Getting Started With Meteor and I'm really not getting far because simple errors keep blocking me. At this point in time I've started writing the initial app in the book in ...

I'd like to get the number of characters between the start of a container and the cursor (including tags characters). Example : <div id="contentEditor" contenteditable="true"> <p>...

I'd like to get the number of characters between the start of a container and the cursor (including tags characters). Example : <div id="contentEditor" contenteditable="true"> <p>...

I'm fairly new to Node.js and am having trouble understanding the way to go about loading libraries or files, in runtime. Apparently, it is a bad idea to load files in runtime using Node.js's native "...

I'm fairly new to Node.js and am having trouble understanding the way to go about loading libraries or files, in runtime. Apparently, it is a bad idea to load files in runtime using Node.js's native "...