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

167
Views
How the return value in promise inside a function works(Learning js for first time)

I recently started learning JS and saw a tutorial on promises, and during a handson exercise i tried writing my own promise.



function isEven(number){
    return new Promise((resolve,reject)=>{
        res = "";
        if(isNaN(number)){
            reject();
            return res;

        }
        else{ 
            if(number%2 == 0){
                res= "Even";
            }
            res = "odd";
            resolve();
            return res;
        }
    })
}
var num = 45
let str = isEven(num).then(()=>{
    console.log("succesfull");
}).catch(()=>{
    console.log("NAN");
})
document.getElementById('number').innerHTML = str;

and the response i got [object Promise] . I want to access the str value which is returned through function isEven , kindly someone help me to understand this concept

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your problem is that, while you're resolving/rejecting the promise under certain conditions, there are no values associated with that action. You need to pass whatever you want to grab to those methods as below:

function isEven(number){
    return new Promise((resolve,reject)=>{
        res = "";
        if(isNaN(number)){
            reject(res)
        }
        else{ 
            if(number%2 == 0){
                res= "Even";
            }
            res = "odd";
            resolve(res)
        }
    })
}
var num = 45
let str = isEven(num).then(()=>{
    console.log("succesfull");
}).catch(()=>{
    console.log("NAN");
})
document.getElementById('number').innerHTML = str;

If you use the .then on a Promise it auto assumes that said Promise will resolve, taking that resolved value as input for whatever callback function you're putting inside

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