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
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
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!