In the following code:
function getPosition() {
return new Promise((res, rej) => {
navigator.geolocation.getCurrentPosition(res, rej);
});
}
function main() {
getPosition().then(console.log);
}
main();
I'm able to log the Position object in the browser. But what if instead of logging it I want to return that value so that it can be used outside of the then(). Is that possible?
I tried the following:
function main() {
return getPosition().then();
}
console.log(main());
But instead of viewing my Position object in the log, I see the following:
Promise {<pending>}
__proto__: Promise
[[PromiseStatus]]: "resolved"
[[PromiseValue]]: Position
This doesn't work either
function main() {
getPosition().then(function(response){
return response;
});
}
console.log(main());
I got undefined in the log.