Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

return a boolean value with a promise

here is my code:

const executorFunction = (resolve, reject) => {

<script>
      
if (  1==1){

    resolve(true);
   
}
else{
    resolve(false);
}

    }




const myFirstPromise = new Promise(executorFunction);


console.log(myFirstPromise);


        </script>

Here is the output of my code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Promise {<fulfilled>: true}
[[Prototype]]: Promise
[[PromiseState]]: "fulfilled"
[[PromiseResult]]: true

I want the boolean value true in the variable myFirstPromise

and i want this output:

true

What is the solution please?

>Solution :

You need to use then. Also the script tag inside the executorFunction function does not make any sense

const executorFunction = (resolve, reject) => {
  if (1 === 1) {
   resolve(true);
  } else {
    resolve(false);
  }

}
const myFirstPromise = new Promise(executorFunction);

myFirstPromise.then(d => console.log(d))

how I can get the boolean value true in a variable out of the function

That may not be possible directly with Promise, instead you can use async function. Internally this also returns a Promise

function executorFunction() {
  return 1 ? true : false;
}

async function getVal() {
  const val = await executorFunction();
  console.log(val)
}

getVal()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading