I am learning promise in JavaScript but I don't understand why my function doesn't return the right value.
This is my function:
function promise() {
let p = new Promise((resolve, reject) => {
return resolve(["a", "random", "array"]);
});
p.then((response) => {
return response;
}).catch((error) => {
return error;
});
}
console.log(promise());
If I run the code I get undefined
.
I think the problem is that the console.log
is executed before the value of the .then
is returned.
Can somebody please help me to solve this problem and maybe explain the problem? :)
Thanks for every answer.