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

174
Visualizações
How do I know if code is asynchronous or not

I wrote an async-await functions using Promise returning functions. However, I'm not sure if the behavior I desire is being implemented. On that token, I've tried researching ways to see if my code is behaving asynchronously but to no avail. Without further ado, here is my code :

sum = 0

function checkEven(num) {
    return new Promise((res,rej) => {
        if(typeof num === 'number'){
            if(num % 2 == 0){
                res(true)
                console.log(`Currently processing ${num}`)
            } else {
                res(false)
            }
        } else {
            rej(`${num} is not a number!`)
        }
    })
}

function addToSum(bool, num) {
    return new Promise((res,rej) => {
        if(bool){
            sum += num
            res(`Adding ${num} to sum. Sum is now ${sum}.`)
        }else{
            res(`Not adding ${num} to sum. Sum is now ${sum}.`)
        }
        rej("hi")
    })

}

async function addEvenNumbersOnly(n1,n2) {
    try{
        const first = await checkEven(n1)
        const second = await checkEven(n2)
        const addFirst = await addToSum(first, n1)
        console.log(addFirst)
        const addSecond = await addToSum(second, n2)
        console.log(addSecond)
        console.log(sum)
    } catch (err){
        console.log(err)
    }
    
}

addEvenNumbersOnly(100,102)

Hopefully the code is self explanatory; I take two numbers and check their evenness, if they're even I add them to a global sum, otherwise I don't.

The asynchronous behavior in my head is here:

const first = await checkEven(n1)
const second = await checkEven(n2)

Does checkEven(n2) execute while checkEven(n1) is being processed?

const addFirst = await addToSum(first, n1)
const addSecond = await addToSum(second, n2)

Similarly, does addToSum(second, n2) start execution while addToSum(first, n1) is being executed?

about 4 years ago · Juan Pablo Isaza
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