How to loop through GET/POST calls sequentially (waiting for previous) return?

后端 未结 2 1531
猫巷女王i
猫巷女王i 2021-01-25 08:17

I\'m writing a Tampermonkey script for a web page and trying to extract data from other pages.
I\'m trying to make a function that has a loop inside that goes thru a list, <

2条回答
  •  一向
    一向 (楼主)
    2021-01-25 08:42

    Simulate for-loop with async ajax requests. On ajax's complete callback go to next item in the list:

    function F_Company_LLC(llcList) {
        var i= 0;
    
        function getNext() {
    
               if(llcList[i][2]=="lab"){
                    //run function 0
    
                        ++i;
    
                        getNext();
                }
                else if(llcList[i][2]=="shop"){
                    //run function 1
    
                        ++i;
    
                        getNext();
                }
                else{
    
                 $.ajax({
    
                  url: base_link+"/company/edit_company/"+llcList[i][1], //CompID
                  method: 'GET',
                  async: true,
                  success: function(data) {
                    if (data.status == "success" && i <= llcList.length) {
                         //data processing
                     }
                   },
                   error: function(xhr) { 
                    alert("Error while processing CompID: " + llcList[i][1]);
    
                   },
                  complete: function() {
                 //complete executes after either 
                 //the success or error callback were executed.
                         ++i;
                        getNext();//go to next item in the list
                  },
                 });      
                }  
        }
    
        getNext();
    }
    
    
    F_Company_LLC(llcList);
    

提交回复
热议问题