geekdu972
geekdu972

Reputation: 199

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:

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?

Upvotes: 0

Views: 2914

Answers (1)

brk
brk

Reputation: 50346

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()

Upvotes: 6

Related Questions