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

96
Visualizações
Use Ajax response to skip a loop iteration in Jquery

I am trying to skip a loop iteration based on the ajax response. The problem I'm facing is that I'm unable to use the response from Ajax outside the Ajax call. Currently I solved it by using the below solution but here the problem is that I'm using async: false which is not recommended as Ajax call is async.

function ajaxCallHere(name) {
    let url = '/admin/someUrl/' + name;
    var skipName = '';
    $.ajax({
        'url': url,
        'type': 'GET',
        async: false,
        'success': function (data) {
            skipName = data.skip;
        }
    });
    return skipName;
}

function getAllNames() {
    for (let i = 0; i < names.length; i++) {
        var skipVal = ajaxCallHere(names.fullName);

        if(skipVal == 'yes') {
            continue;
        }
    }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Use async/await, since $.ajax returns an object that obeys the Promise protocol.

function ajaxCallHere(name) {
  let url = '/admin/someUrl/' + name;
  return $.get(url);
}

async function getAllNames() {
  for (let i = 0; i < names.length; i++) {
    var data = await ajaxCallHere(names.fullName);
    if (data.skipVal == 'yes') {
      continue;
    }
  }
}

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