How do I do consistent AJAX in a loop?

  • 0
    Hello! How do I organize sequential AJAX requests in a loop. I guess that you need to use async and await (so that the next request starts after the previous one completes), but I can't organize this. Thanks in advance!

    $('#form').on('submit', function(e){
    
        e.preventDefault();
    
        var numberOfRows = table.data().length; //Получаем количество строк в таблице
    
        for (var i = 0; i < numberOfRows; i++) {
          const trElem = table.row(i).node();
          const $rowControls = $('input,select,textarea', trElem);
          const serialized = $rowControls.serialize();  //Получаем в цикле с каждой строки данные для отправки
    
          async function SendRowData() {
         
         }
          }
    });
    JavaScript Anonymous, Mar 9, 2020

  • 1 Answers
  • 0
    $('#form').on('submit', async function(e) {
    ...
    for (var i = 0; i < numberOfRows; i++) {
    ...
    const response = await fetch(...);
    ...
    }
    }
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!