Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

223
Visualizações
Why can't I turn my function to work synchronous with Async Await

I have an html page which displays e-commerce orders. I select some of them with checkboxes, and create invoices using an ajax request to php server.

Now I want to create these invoices 3 by 3. So for example, if I have 4 invoices, I will make a ajax request 2 times, and show the process to user. Below is my code , I implemented async/await. However it is not working sync. I run below code for 4 invoices.

The output I expect should be in javascript console:

"Hello"
"Bye"
"Hello"
"Bye"

But it is printing to console:

"(2)Hello"
"(2)Bye"

What am I missing ? Here is the code:

  function processInvoices(){

  var arr_ordersToBeProcessed = [];

  let bulkCount = 3;
  let currCount = 0;
  let totalCount = jQuery("#div_orders").find('.invoiceCheckbox:checked').length;

  jQuery("#div_orders").find('.invoiceCheckbox').each(async function(i, obj) {

    let clickedOrder = $(obj).parent().parent().parent();

    if($(obj)[0].checked == true){

      let obj_singleOrder = {};

      obj_singleOrder.orderNumber = clickedOrder.find('.div_orderNumber').text();
      arr_ordersToBeProcessed.push(obj_singleOrder);

      currCount++;

      if(currCount%3==0 || currCount==totalCount){
          console.log("hello");
          const data = await jQuery.ajax({
                 type: "POST",
                 url: "./_production/createInvoices.php",
                 data: {
                   orders: JSON.stringify(arr_ordersToBeProcessed)
                 }
           });
          arr_ordersToBeProcessed = [];
          console.log("bye");

          if(currCount==totalCount){
            alert("Completed!"); return;
          }
      }
    }
  });
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You are misunderstanding what "await" does. It does not make your call synchronous. It simply lets you write Code differently so that multiple asynchronous function calls in a row do not cause you Code to cascade into hell.

If you want to achieve a synchronous http call using jquery it can be done by adding

async: false

(As described here How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request? ).

I would however strongly advise against using synchronous http calls since it will make your website unresponsive while the call is being answered.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda