JavaScript wait for execution of two ajax requests success functions in wait execution ajax javascript,wait for ajax execution

I have two ajax calls that are using GET, each one having a different success function.

I want to wait for the response of both requests and the execution of both success functions and then I need to execute another function?

How can I obtain this behavior?

I tried using $q.all but what this does is to wait for ajax requests to finish and then calls my third function. The problem with this approach is that it doesn't execute the two functions attached for the two ajax requests (it executes the third function and then the other two).

Any ideas?

Answer:1

Use jQuery Promises,which provide a better and more flexible solution for this type of problem.

Demo code:

 var promiseOne, promiseTwo;
    // Promises
    promiseOne = $.ajax({ url: '../test.html' });
    promiseTwo = $.ajax({ url: '../test.html' });

   // $.when() creates a new promise which will be:
    // resolved if both promises inside are resolved
    // rejected if one of the promises fails
    $.when(
        promiseOne,
        promiseTwo
    )
    .done(function () {          //.done() is callback for resolve state
        console.log('promiseOne and promiseTwo are done');
    })
    .fail(function () {   //.fail() is callback for reject state 
        console.log('One of our promises failed');
    });

Instead of using .done() & fail(), use can use .then() which also return a promise.

Answer:2

Do you use a transpiler like babel? If so, I would recommend to use the yield syntax.

[this.scope.result1, this.scope.result2] =
          yield [
            this.myService.getResult1(),
            this.myService.getResult()
          ]

 console.debug("Here, we have both the results ready")

If not, you probably have to do it manually either using a counter or chain the callbacks.

Answer:3

I will to do it with angular $watch. $watch call a given fonction each time the watched variable(s) is/are modified :

$scope.req1ok = false ;  
$scope.req1ok = false ;  

$http(request1).success(function (status) {
    $scope.req1ok = true ;  
}).error(function (status) {
    ...
});

$http(request2).success(function (status) {
    $scope.req2ok = true ;  
}).error(function (status) {
    ...
});

$scope.$watchCollection('[req1ok,req2ok]', function () {
    // when request1 and request2 done complety
    if($scope.req1ok && $scope.req2ok){
      // do what you want
    }
});
Answer:4

I am using an object to store state and the object looks like this: { "CPC": 0, "NDP": 0, "LPC": 0, "GPC": 1, "PPC": 2 } Where all the key values are an enumerator of a generic type <T&...

I am using an object to store state and the object looks like this: { "CPC": 0, "NDP": 0, "LPC": 0, "GPC": 1, "PPC": 2 } Where all the key values are an enumerator of a generic type <T&...

  1. total number of objects in an area
  2. total number of objects in active directory
  3. total number of objects in a database
  4. total number of objects in s3 bucket
  5. total number of objects expired tsm
  6. total number of objects in a set
  7. total number of objects in a database 32 768
  8. total number of objects in a database access
  9. total number of objects expired
  10. total number of standard objects in salesforce
  11. find total number of objects in active directory
  12. get total number of objects in json
  13. total number of methods in object class in java
  14. tsm total number of objects inspected
  15. total number of objects inspected
  16. total number of objects encrypted
  17. the total number of objects

I'm trying to call a rest service from jQuery using the code below $.ajax({ type: verb, url: url, headers: header, crossDomain: true, xhrFields: { withCredentials: true ...

I'm trying to call a rest service from jQuery using the code below $.ajax({ type: verb, url: url, headers: header, crossDomain: true, xhrFields: { withCredentials: true ...

  1. php send arabic characters

I am new to coding and trying to get this problem right. I tried calling the index of the arrays but it prints out columns. Out dev environment is REPL.it, the virtual machine uses node v10.16.0 and ...

I am new to coding and trying to get this problem right. I tried calling the index of the arrays but it prints out columns. Out dev environment is REPL.it, the virtual machine uses node v10.16.0 and ...

  1. array make first element

I'm a developer for the website Friconix, a collection of free icons. Users can include our JS file on their web pages as explain here : https://friconix.com/start/. I would like to gather statistics ...

I'm a developer for the website Friconix, a collection of free icons. Users can include our JS file on their web pages as explain here : https://friconix.com/start/. I would like to gather statistics ...