here is my code:
const executorFunction = (resolve, reject) => {
if (1 === 1) {
resolve(true);
} else {
resolve(false);
}
}
const myFirstPromise = new Promise(executorFunction);
console.log(myFirstPromise)
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?
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()