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

177
Visualizações
SOLVED - Async function firing inside if-statement when it shouldn't

my isValid function was returning string so it was always true, while in the console i was getting true/false

I have function when the input-1 element changes, if the user input is valid it should fire an async function that fetches data and populates datalist for input-2.

Even tho const validInput = isValid(input1); return correct result, the async function always get executed

input1.addEventListener('change', handleChange)
    
function handleChange () {
    
    const validInput = isValid(input1); // correctly returns true/false
console.log(validInput)
    if(!validInput) {   
        hideInput(input2);
console.log('1',validInput)
    }
    else {
console.log('2',validInput)
        updateInput2datalist(); // gets executed either way
        displayInput(input2);
    }
};
async function updateInput2datalist () {
    let arr; 
    await getModelsAPI().then(response => arr = response);
    populateDatalist(input2Datalist, arr);            
}

from the console i get true 2 true

false 2 false

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I think you need to reverse the if condition, as updateInput2datalist function will be executed on validInput equal true value.

if(validInput) {   
    hideInput(input2);
}
else {
    updateInput2datalist(); // gets executed either way
    displayInput(input2);
}
about 4 years ago · Santiago Trujillo 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