jQuery run function after $.each ajax request has completed

leaksterrr

I had a simple script working using Promises in ECMA6 but I'm now converting what I wrote to jQuery to be compatible for all browsers but the issue I'm having is that availableDevices contains an array of AJAX responses rather an array of product names like the script is telling it to.

Basically when all AJAX requests inside $.each have finished, I need to run a function with that data. What am I missing? (It's been a while since I wrote anything with jQuery...)

var availableDevices = [];

$.each(products, function (index, product) {

    availableDevices.push($.ajax({
        type: 'GET',
        url: 'http://localhost:1337/api/product_availability?__url_path_param=' + formatProductName(product.product_name),
        complete: function (response) {
            if (response.status == 200) {
                return formatProductName(product.product_name);
            }
        }
    }));

});

$.when(availableDevices).then(function (data) {

    console.log(data);

});
Jamiec

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/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

callback to function after .each() has completed?

From Dev

in jquery how to call a function after an ajax request has been sent?

From Dev

jQuery function to sometimes redirect after an AJAX Request is completed and to sometimes remain on the page?

From Dev

Nested jQuery each statement runs after the first has completed

From Dev

jQuery run function after everything has run

From Dev

jQuery: Run function if each tr has class

From Dev

Run simultaneous tasks after a BackgroundWorker has completed

From Dev

Call a function after another function has completed

From Dev

How to execute a function only after a request.execute to gapi.client.youtube has completed?

From Dev

Run a callback after multiple function have completed

From Dev

Jquery run code after ajax in .each() loop finishes

From Dev

Jquery after Ajax request

From Dev

Run a function after each function

From Dev

jQuery Mobile execute a function after another is completed

From Dev

jQuery hide div after function is completed

From Dev

Jquery. Calling a function after .each loop with ajax calls

From Dev

React - Set State after every forEach API Request has completed

From Dev

jQuery code after ajax runs before ajax is completed

From Dev

jQuery $when.apply() .. then() firing for each completed request

From Dev

Processing after the request was completed

From Dev

jQuery wait for an AJAX request to be completed before refreshing the page

From Dev

ajax request after jquery validation

From Dev

How to call a function only after a SequentialTransition in JavaFX has been completed?

From Dev

Start a second function, after the first one has been completed.

From Dev

Failing to delay function until after 'for' loop has fully completed

From Dev

Run a javascript function after ASP.NET page load is completed

From Dev

jQuery Ajax Wait Each Function

From Dev

Check if function is jQuery ajax request

From Dev

How can I run a javascript function only after a $http request has been made in Angular?

Related Related

  1. 1

    callback to function after .each() has completed?

  2. 2

    in jquery how to call a function after an ajax request has been sent?

  3. 3

    jQuery function to sometimes redirect after an AJAX Request is completed and to sometimes remain on the page?

  4. 4

    Nested jQuery each statement runs after the first has completed

  5. 5

    jQuery run function after everything has run

  6. 6

    jQuery: Run function if each tr has class

  7. 7

    Run simultaneous tasks after a BackgroundWorker has completed

  8. 8

    Call a function after another function has completed

  9. 9

    How to execute a function only after a request.execute to gapi.client.youtube has completed?

  10. 10

    Run a callback after multiple function have completed

  11. 11

    Jquery run code after ajax in .each() loop finishes

  12. 12

    Jquery after Ajax request

  13. 13

    Run a function after each function

  14. 14

    jQuery Mobile execute a function after another is completed

  15. 15

    jQuery hide div after function is completed

  16. 16

    Jquery. Calling a function after .each loop with ajax calls

  17. 17

    React - Set State after every forEach API Request has completed

  18. 18

    jQuery code after ajax runs before ajax is completed

  19. 19

    jQuery $when.apply() .. then() firing for each completed request

  20. 20

    Processing after the request was completed

  21. 21

    jQuery wait for an AJAX request to be completed before refreshing the page

  22. 22

    ajax request after jquery validation

  23. 23

    How to call a function only after a SequentialTransition in JavaFX has been completed?

  24. 24

    Start a second function, after the first one has been completed.

  25. 25

    Failing to delay function until after 'for' loop has fully completed

  26. 26

    Run a javascript function after ASP.NET page load is completed

  27. 27

    jQuery Ajax Wait Each Function

  28. 28

    Check if function is jQuery ajax request

  29. 29

    How can I run a javascript function only after a $http request has been made in Angular?

HotTag

Archive