Multiple async calls async: How to execute a function after all calls are resolved, even if some of them do not work?

Let's say I have an array of so-called ajax calls:

// an array of ajax calls
var callsArray = [];
callsArray.push(
  $.ajax({
    url: '/url1';
    cache: false,
    dataType: 'html'
  }),
  $.ajax({
    url: '/url2';
    cache: false,
    dataType: 'html'
  })
)

I know in advance that at least one of these two calls will fail. I want to execute the function after the BOTH calls resolved both with success and with an error, and I also want to log any failures.

This will not work:

// NOT APPROPRIATE
var errors = '';
$.when.apply(null, callsArray)
  .done(function() {
     console.log('Both calls completed but these urls failed: ' + errors);
  })
  .fail(function() {
     errors += this.url + ' ';
  })

The problem with the above is that .fail is executed if even one call fails, and .done is only executed if null calls fail. Also I can not use. Always, because it is executed as soon as any call is resolved.

So I'm looking for something like this:

// FANTASY CODE
var errors = '';
$.when.apply(null, callsArray)
  .allCallsResolved(function() {
     // this executes only when all calls have
     // returned either a success or a failure
     // rather than when all calls succeed
     console.log('All calls completed but these urls failed: ' + errors);
  })
  .everyFailure(function() {
     // this executes every time a call fails
     // rather than as soon as any call fails 
     errors += this.url + ' ';
  })
+4
source share
3

ajax Promise, , , Promise.all, , / :

const promises = callsArray.map(function(call) {
    return new Promise(function(resolve, reject) {
        call.done(resolve).fail(function(error) {
            console.log(error);
            resolve(error);
        });
    });
});

Promise.all(promises).then(function(values) {
     console.log(values);
     //all calls done or failed
});
+1

var callsArray = [];
callsArray.push(
  $.ajax({
    url: 'https://httpbin.org/status/404',
    cache: false,
    dataType: 'html'
  }),
  $.ajax({
    url: 'https://httpbin.org/status/200',
    cache: false,
    dataType: 'html'
  })
);

callsArray.forEach((e) => {
  e.done(() => console.log('done'))
    .fail(() => console.log('fail'))
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
0

I did this using the Snooze function in jQuery.

     var diff_array = [];
        var count=0;//you may use counter to know how many Deferred objects are resolved/rejected.

        //Iterate your callsArray 

        for(i in callsArray){

        diff_array[i] = $.Deferred();
        //execute each ajax
        //if ajax fails call reject else resolve/promise.
        diff_array[i].resolve();
        //count++ based on resolve/reject
        }

        //any where in the script
        // write deferred.done() which will be called immediately on resolving corresponding deferred object.
        diff_array[13].done(
        function() {
        .
        .
          if(count=callsArray.length){
           callFinalFunction();
           }
        }
        );
-1
source

Source: https://habr.com/ru/post/1687535/


All Articles