简体   繁体   English

在 for 循环中嵌套调用 ajax function

[英]Nested ajax function calls inside a for loop

I have looked into many solutions which uses deffered and promises.Tried those solutions in my scenario but couldn't got a solution.I have been fighting with through out the entire day.My scenario is.我研究了许多使用延迟和承诺的解决方案。在我的场景中尝试了这些解决方案但无法找到解决方案。我一整天都在努力。我的场景是。

I have array of customers.And i need to do some certain actions to each customer one by one which are defined as ajax.我有很多客户。我需要对每个客户进行一些特定的操作,这些操作被定义为 ajax。

private function main()
{
    customers=["customer1","customer2"]
    customers.forEach(function(customer) {

        function1();
        function2();
        function3();

    }
}
private function function1()
{
     $.ajax({
                type: "POST",
                url: Url1,
                data: {},
                success: function (data) {
                    console.log("a");
                },
                dataType: 'JSON'
            });
}
private function function2()
{
     $.ajax({
                type: "POST",
                url: Url2,
                data: {},
                success: function (data) {
                    console.log("b");
                },
                dataType: 'JSON'
            });
}
private function function3()
{
     $.ajax({
                type: "POST",
                url: Url3,
                data: {},
                success: function (data) {
                    console.log("c");
                },
                dataType: 'JSON'
            });
}

When the main function is called.My desired output is当调用主 function 时。我想要的 output 是

a
b
c
a
b
c

But the output i am getting is但是我得到的 output 是

a
a
b
b
c
c

Please help me find a solution.请帮我找到解决办法。

Ajax call by default is the async. Ajax 调用默认是异步的。 action.行动。

function send() {
  fetch(
    url,
    {
      method: 'post',
      header: {'Content-Type' : 'application/text'},
      body: 'a'
    }
  ).then(
    () => {
      console.log('a is sended');
      fetch(
        url,
        {
          method: 'post',
          header: {'Content-Type' : 'application/text'},
          body: 'b'
        }
      ).then(
          () => {
            console.log('b is sended'); 
            fetch(
              url,
              {
                method: 'post',
                header: {'Content-Type' : 'application/text'},
                body: 'c'
              }
            ).then( () => console.log('c is sended') )
          }
        )
    }
  )
}

Finally i solved my issue by making a recursive function call without using loop.最后,我通过在不使用循环的情况下进行递归 function 调用解决了我的问题。

var customers=["customer1","customer2"];
var currentIndex=0;
private function main()
{
  if(customers[currentIndex]){
   function1().done(function(data) {
     console.log("a");
      function2().done(function(data) {
        console.log("b");
        function3().done(function(data) {
          console.log("c");
           currentIndex++; 
           main();
        });
      });
   });
   
  }
}
private function1()
{
  return  $.ajax({
                type: "POST",
                url: url1,
                data: {},
                dataType: 'JSON'
            });
}
private function2()
{
  return  $.ajax({
                type: "POST",
                url: url2,
                data: {},
                dataType: 'JSON'
            });
}
private function3()
{
  return  $.ajax({
                type: "POST",
                url: url3,
                data: {},
                dataType: 'JSON'
            });
}

And current output is而当前的 output 是

a
b
c
a
b
c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM