c# .net Adsense ADO.NET Linq Viruses/security asp.net MVC JQuery Angular-js Node-js SEO Java C++ SQL API Networking vb.net .Net Css JavaScript Generics c#.Net entity framework HTML Website host Website Construction Guide HTTP tutorial W3C tutorial Web Services JSON Psychology Ionic framework Angular ReactJS Python Computer Android
JQuery

How to execute asynchronous ajax callback function in order

| | JavaScript , JQuery

Previously encountered a business needs, the need to rely on the order to perform an unlimited number of ajax request to the server.

We know that in general JS asynchronous callback ajax function execution order cannot be guaranteed. 
I used the queues and recursive methods based on jQuery to initially implement callback functions that sequentially execute asynchronous ajax requests. 
If wrong, please criticize and correct :)

<script type="text/javascript">

 

    $(document).ready(function() {

 

        // Executes multipleajax commands in order, using a recursion because of the indefinite number 

        function  send (action, arg2) { 

            // Encapsulate multiplecommands into array objects in sequence and execute them recursively 

            // Use the deferredobject to control the callback function 

            $ .when (send_action (action [0],arg2)) 

                .done ( function  () { 

                    //Before ajax callback function to determine the length of the queue 

                    if  (action.length> 1) { 

                        //queue length greater than 1, the first pop-up, continue to recursively executethe queue 

                        action.shift (); 

                        send (action,arg2); 

                    } 

                }). fail ( function  () { 

                    //Queue elements in the request after the failure of the logic 

                    // 

                    //retry sending 

                    //send (action, arg2); 

                    // 

                    //Ignore the error before proceeding 

                    // if(action.length> 1) { 

                    //queue length greater than 1, the first pop-up, continue to recursively executethe queue 

                    //action.shift (); 

                    //send (action, arg2); 

                    //} 

                }); 

        } 

 

        // handle each command'sajax request and callback function 

        function  send_action (command, arg2) { 

            var  dtd = $ .Deferred ();

            $ .post ( 

                "url"

                { 

                    command: command, 

                    arg2: arg2 

                } 

            ) .done ( function  (json) { 

                json = $ .parseJSON(json); 

                // Processinglogic for each callback function 

                // 

                // 

                // 

                // Logicalend 

                dtd.resolve (); 

            }). fail ( function  () { 

                // ajax The logicof the request failed    

                dtd.reject (); 

            }); 

            return  dtd.promise (); //Returns the promise of a Deferred object, preventing external modification ofthe state 

        } 

    });

 </script>