SoFunction
Updated on 2025-03-10

angularjs summarizes methods for handling multiple asynchronous requests

In actual business, you often need to wait for several requests to complete before proceeding with the next step. But $http in angularjs does not support synchronous requests.

Solution 1:

Copy the codeThe code is as follows:

$('url1').success(function (d1) {
        $('url2').success(function (d2) {
//Processing logic
        });
    });

Solution 2:

The methods in then will be executed in order.

Copy the codeThe code is as follows:

var app = ('app',[]);
('promiseControl',function($scope,$q,$http) {
    function getJson(url){
        var deferred = $();
        $(url)
            .success(function(d){
                d = parseInt(d);
                (d);
                (d);
            });
        return ;
    }

    getJson('').then(function(){
        return getJson('');
    }).then(function(){
        return getJson('');
    }).then(function(){
        return getJson('');
    }).then(function(d){
        ('end');
    });
});

Solution three:

The first parameter of the $ method can be an array (object). After all the contents in the first parameter are executed, the method in then will be executed. All return values ​​of the method of the first parameter are passed in as an array (object).

Copy the codeThe code is as follows:

var app = ('app',[]);
('promiseControl',function($scope,$q,$http) {
    $({first: $(''),second: $('')}).then(function(arr){
        (arr);
        (arr,function(d){
            (d);
            ();
        })
    });
});

There are many tutorials on the detailed usage of $q. I'm just getting in touch too. If you can't speak well, you dare not talk nonsense. The above code was written according to my understanding, and there was no problem after testing.