Jquery Ajax prevents delayed sequential loop failure

So, I am connecting serial ajax to load an array of URLs in order. I originally used .then()instead .always(), and in any case, it works fine - assuming all URLs exist. However, since it is possible to skip files, I would like to compensate for this, and then finally inform the user about which files were missing in order to simplify its correction.

However, the problem is that the code is executed on the missing / 404 file as it should, but then exits the loop, preventing further ajax calls. Therefore, I believe that I need some way of processing fail()and enforced success, regardless of another or another way of overriding the default behavior to 404, so it continues to evolve through the loop.

Unfortunately, the closest Google results are how to do the opposite (force failure upon success).

var missing=[];
uLoadList.reduce(function(prev, cur, index) {
    return prev.then(function(data) {
        return $.ajax("/wiki/"+cur).always(function(data) {
            var temp = $('#mw-content-text',data);
            temp = $('pre',temp);
            if(temp.length > 0)
            {
                //handle success
            }else{
                //handle failure
                missing.push(cur);
            }
        });
    });
}, $().promise()).done(function() {
    if(missing.length > 0)
    {
        //notify of missing objects
    }
    //continue on.
});

Finally, to ease confusion: the URLs and the script itself are on the MediaWiki site - so even if 404 is returned, there will always be page content and will contain an element with the identifier "mw-content-text".

+1
source share
1

Try

(function ($) {
    $.when.all = whenAll;
    function whenAll(arr) {
        "use strict";
        var deferred = new $.Deferred(),
            args = !! arr 
                   ? $.isArray(arr) 
                     ? arr 
                     : Array.prototype.slice.call(arguments)
                       .map(function (p) {
                         return p.hasOwnProperty("promise") 
                         ? p 
                         : new $.Deferred()
                           .resolve(p, null, deferred.promise())
                       }) 
                   : [deferred.resolve(deferred.promise())],
            promises = {
                "success": [],
                  "error": []
            }, doneCallback = function (res) {
                promises[this.state() === "resolved" 
                         || res.textStatus === "success" 
                         ? "success" 
                         : "error"].push(res);
                return (promises.success.length 
                       + promises.error.length) === args.length 
                       ? deferred.resolve(promises) 
                       : res
            }, failCallback = function (res) {
                // do `error` notification , processing stuff
                // console.log(res.textStatus);
                promises[this.state() === "rejected" 
                        || res.textStatus === "error" 
                        ? "error" 
                        : "success"].push(res);
                return (promises.success.length 
                       + promises.error.length) === args.length 
                       ? deferred.resolve(promises) 
                       : res
            };
        $.map(args, function (promise, index) {
            return $.when(promise).always(function (data, textStatus, jqxhr) {
                return (textStatus === "success") 
                    ? doneCallback.call(jqxhr, {
                        data: data,
                        textStatus: textStatus 
                                    ? textStatus 
                                    : jqxhr.state() === "resolved" 
                                      ? "success" 
                                      : "error",
                        jqxhr: jqxhr
                      }) 
                    : failCallback.call(data, {
                        data: data,
                        textStatus: textStatus,
                        jqxhr: jqxhr
                      })
            })
        });
        return deferred.promise()
    };
}(jQuery));
// returns `Object {
//                   success: Array[/* success responses*/], 
//                   error: Array[/* error responses */]
//          }`

// e.g.,
var request = function (url, data) {
    return $.post(url, {
        json: JSON.stringify(data)
    })
}, settings = [
    ["/echo/json/", "success1"], // `success`
    ["/echo/jsons/", "error1"], // `error`
    ["/echo/json/", "success2"], // `success`
    ["/echo/jsons/", "error2"], // `error`
    ["/echo/json/", "success3"] // `success`
];

$.when.all(
  $.map(settings, function (p) {
    return request.apply($, p)
  })
)
.then(function (data) {
    console.log(data);
    // filter , process responses
    $.each(data, function(key, value) {
        if (key === "success") {
          results.append(
              "\r\n" + key + ":\r\n" + JSON.stringify(value, null, 4)
          )
        } else {            
          results.append(
              "\r\n" + key + ":\r\n" 
              + JSON.stringify(
                  value.map(function(v, k) {
                    v.data.responseText = $(v.data.responseText)
                      .filter("title, #summary, #explanation")
                      .text().replace(/\s+/g, " ");
                    return v
                  })
                , null, 4)
          )
        }
    })
}, function (e) {
    console.log("error", e)
});

jsfiddle http://jsfiddle.net/guest271314/620p8q8h/

+1

All Articles