Reputation: 67
in this sample rez contains pending Promise. I need to get it resolved with other code paused till it's get resolved:
function getCoordinates() {
return new Promise(function (resolve, reject) {
navigator.geolocation.getCurrentPosition(resolve, reject);
});
}
async function getAddress() {
await getCoordinates();
}
const rez = getAddress();
alert('wait for rez');
I know I can do something like getAddress().then(function () { alert('wait for rez')} );
but is there a way to continue not inside of then
scope?
Upvotes: 0
Views: 171
Reputation: 35573
You need to await
the async operation as well:
(async function(){
function getCoordinates() {
return new Promise(function (resolve, reject) {
navigator.geolocation.getCurrentPosition(resolve, reject);
});
}
async function getAddress() {
await getCoordinates();
}
const rez = await getAddress();
alert('wait for rez');
})()
Upvotes: 0
Reputation: 67
As was commented functions wich calls promise needs to be async
otherwise it returns pending promise.
$(window).load(async function () {
var rez = await getCoordinates();
alert(rez.coords.latitude);});
function getCoordinates() {
return new Promise(function (resolve, reject) {
navigator.geolocation.getCurrentPosition(resolve, reject);
});};
Upvotes: 0