如何在http调用成功之前调用http循环内的for循环?

问题描述:

我想完成后的第一次调用API是success.But在我的代码是调用API的第一个完成如何在http调用成功之前调用http循环内的for循环?

for(var w=0;w<Ids.length;w++){ 
     $scope.msgObject = { 
       "SenderID":$scope.pageId, 
       "PageID" : $scope.pageId, 
       "Date":Date.now().toString(), 
       }; 

     $http({ 
      method: 'POST', 
      url: '///url', 
      async:true, 
      data: $scope.msgObject, 
      headers: { 
       'Content-Type': 'application/json' 
      }}) 
      .then(function(response) { 
       console.log("success posting"); 
       } 
      }) 
      .catch(function(response){ 

      }); 
     $(".messageInput").val(''); 


     } 
    } 
    } 
} 
+2

你能否澄清你的问题?循环的每次迭代都会对您的URL启用异步API调用,并且循环不会等待每个响应。每个then()函数中的代码将在响应返回时运行(异步)。这是你所期望的吗? (编辑 - 拼写) –

+0

我认为这是你要找的东西:http://*.com/a/33741475/5547718。在你的情况下,你必须通过简单地推送ajax请求来创建myAsynchFunctions数组。 – ThatBrianDude

+0

这个问题是一个骗局。你会在google上找到答案。这个问题与javascript标签中其他问题的60%相同。 –

之前把你的循环内THEN 像

function urPostMethod(url){ 

$scope.msgObject = { 
       "SenderID":$scope.pageId, 
       "PageID" : $scope.pageId, 
       "Date":Date.now().toString(), 
       }; 

     $http({ 
      method: 'POST', 
      url: url, 
      async:true, 
      data: $scope.msgObject, 
      headers: { 
       'Content-Type': 'application/json' 
      }}) 
      .then(function(response) { 
       console.log("success posting"); 
       while(Ids.length>0)urPostMethod(Ids.pop()); 
       } 
      }) 
      .catch(function(response){ 

      }); 
     $(".messageInput").val(''); 


     } 
    } 
    } 
} 
+0

但是如何停止第二个API调用 – kragor

+0

直到第一次调用完成后才会调用第二个API – uonlyYOLOonce

你要做的是将异步和同步操作混合在一起,这根本不合逻辑。

如果需要调用这些API在数组中的元素的顺序,你可以用推迟使用不同的方法像管的要求:

var dfd = $.Deferred(), 
    x = 0, // Loop index 
    Ids = [], 
    msgObject = { 
     "SenderID":$scope.pageId, 
     "PageID" : $scope.pageId, 
     "Date":Date.now().toString(), 
     }; 


    callAjax = function (value) { 
     var dfdAjax = $.Deferred(); 

     $.ajax({ 
      method: 'POST', 
      url: '///url', 
      async:true, 
      data: msgObject, 
      headers: { 
       'Content-Type': 'application/json' 
      }}) 
     .then(function(response) { 
      dfdAjax.resolve(response); 
     }) 
     .catch(function(response){ 
      dfdAjax.reject(response); 
     });   

     return dfdAjax.promise(); 
    }, 

    requestAjax = function (value) { 
     return callAjax(value); 
    }; 

dfd.resolve(); 

for (x = 1; x <= Ids.length; x++) { 

    dfdNext = dfdNext.pipe(function() {   
     return requestAjax(value). 
      done(function(response) { 
       // Process the response here. 
      }); 

    }); 

} 

function asyncForEach(arr, cb) { 
 
    return arr.reduce((p,c)=>{ 
 
     return p.then(()=> cb(c)); 
 
    }, Promise.resolve()); 
 
} 
 

 
function fetch(id) { 
 
    return new Promise(resolve=> 
 
    setTimeout(resolve, 100)) // replace with your AJAX call 
 
     .then(()=>console.log('success posting', id)); 
 
} 
 

 
function done() { 
 
    console.log('all done!'); 
 
} 
 

 
const ids = [1, 2, 3, 4, 5]; 
 
asyncForEach(ids, fetch).then(done);

你可以使用$q.all()并简化你的语法,你只需要注入$q服务。

以下代码将$http返回的所有承诺添加到一个数组中,使用$q.all()执行承诺并收集结果。

var requests = []; 

for(var w = 0; w < Ids.length; w++) {  
    var req = $http({ 
     method: 'POST', 
     url: '///url', 
     headers: { 'Content-Type': 'application/json' }, 
     data: { 
      SenderID: $scope.pageId, 
      PageID: $scope.pageId, 
      Date: Date.now().toString(), 
     } 
    }) 
     .catch(function(err) { 
      // handle err 
     }); 

    requests.push(req); 
} 

$q.all(requests) 
    .then(function (results) { 
     // handle results 
    });;