Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

173
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!