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?
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');
})()
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);
});};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.