Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

176
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda