Javascript 言而有信
应该在3次尝试中检索位置的Javascript 言而有信,javascript,geolocation,promise,async-await,es6-promise,Javascript,Geolocation,Promise,Async Await,Es6 Promise,应该在3次尝试中检索位置的getLocation()函数返回undefinednavigator.geolocation.getCurrentPosition()返回正确的位置,但问题在于承诺处理 问题显然是我把承诺称为承诺中的承诺。我不允许在geologe()中使用wait关键字,该关键字已声明为async 原始通话: var getLocationPromise = this.getLocation(); // Do something... location = await getLoca
getLocation()
函数返回undefined
navigator.geolocation.getCurrentPosition()
返回正确的位置,但问题在于承诺处理
问题显然是我把承诺称为承诺中的承诺。我不允许在geologe()
中使用wait
关键字,该关键字已声明为async
原始通话:
var getLocationPromise = this.getLocation();
// Do something...
location = await getLocationPromise;
getLocation()
:
只要以下内容在声明为async的函数中
var getLocationPromise = this.getLocation();
// Do something...
location = await getLocationPromise;
应该没问题
看看getLocation/geologice,除非您需要一个单独的geologice方法,否则它们应该能够组合并简化为
getLocation() {
var geolocate = () =>
new Promise((resolve, reject) =>
navigator.geolocation.getCurrentPosition(resolve, reject, {
enableHighAccuracy: true,
timeout: 20000,
maximumAge: 1000
});
);
// this function will "retry" the supplied function (fn) cont times
var limitedPromiseRetry = (fn, cont) => fn().catch(err => cont > 0 ? limitedPromiseRetry(fn, cont-1) : Promise.reject('Max number of geolocation attempts'));
return limitedPromiseRetry(geolocate, 3);
}
只要以下内容在声明为async的函数中
var getLocationPromise = this.getLocation();
// Do something...
location = await getLocationPromise;
应该没问题
看看getLocation/geologice,除非您需要一个单独的geologice方法,否则它们应该能够组合并简化为
getLocation() {
var geolocate = () =>
new Promise((resolve, reject) =>
navigator.geolocation.getCurrentPosition(resolve, reject, {
enableHighAccuracy: true,
timeout: 20000,
maximumAge: 1000
});
);
// this function will "retry" the supplied function (fn) cont times
var limitedPromiseRetry = (fn, cont) => fn().catch(err => cont > 0 ? limitedPromiseRetry(fn, cont-1) : Promise.reject('Max number of geolocation attempts'));
return limitedPromiseRetry(geolocate, 3);
}
异步并等待
如果没有async
关键字,则不能在函数内部使用wait
。因此发生错误是因为执行器函数不是异步的:
var getLocation = async function(){ // <-- this function has "async" keyword, but ...
return new Promise( function( resolve, reject ){ // <-- ... this "executor" function has no "async" keyword.
var value = await geolocate(); // <-- ERROR: await is only valid in async function.
resolve( value );
})
};
没有嵌套的承诺
但是由于getLocation
已经是一个承诺,您根本不需要嵌套的新承诺(…)
:
var getLocation = async function(){ // <-- "async" makes a function to be a promise
var value = await geolocate();
// "return value" inside async function is "the same" as
// "resolve( value )" in a promise
return value;
};
异步并等待
如果没有async
关键字,则不能在函数内部使用wait
。因此发生错误是因为执行器函数不是异步的:
var getLocation = async function(){ // <-- this function has "async" keyword, but ...
return new Promise( function( resolve, reject ){ // <-- ... this "executor" function has no "async" keyword.
var value = await geolocate(); // <-- ERROR: await is only valid in async function.
resolve( value );
})
};
没有嵌套的承诺
但是由于getLocation
已经是一个承诺,您根本不需要嵌套的新承诺(…)
:
var getLocation = async function(){ // <-- "async" makes a function to be a promise
var value = await geolocate();
// "return value" inside async function is "the same" as
// "resolve( value )" in a promise
return value;
};
等待getLocation()。然后()代码>=等待getLocation()代码>-不是吗?它会给出不同的结果。我遇到了await getLocation()的问题代码>返回未定义的,因此我使用等待getLocation()。然后()
这似乎很安全不确定为什么在Geologe
和getLocation
上使用async
-因为这两个函数都不使用await
//不能在async内部使用await
-不,这不对,只能在async
内部使用await
,这就是问题所在,不能在async
中等待
等待getLocation()。然后()代码>=等待getLocation()代码>-不是吗?它会给出不同的结果。我遇到了await getLocation()的问题代码>返回未定义的,因此我使用等待getLocation()。然后()
这似乎很安全不确定为什么在Geologe
和getLocation
上使用async
-因为这两个函数都不使用await
//不能在async内部使用await
-不,这不对,只能在async
内部使用await
,这就是问题所在,不能在async
Nice和compact解决方案中wait
,它避免了使用双重承诺。从您的回复中,我了解到将承诺放在承诺中不是一个好主意,也不受支持?不,它是受支持的,只是在这种情况下不需要它。一个简洁的解决方案,它避免了使用双重承诺。从您的回复中,我了解到在承诺中加入承诺不是一个好主意,也不受支持?不,这是受支持的,只是在这种情况下不需要
var nestedPromises = new Promise( function( resolve1, reject1 ){
resolve1( new Promise( function( resolve2, reject2 ){
resolve2( new Promise( function( resolve3, reject3 ){
resolve3('resolved value');
}));
}));
});
nestedPromises.then( function( value ){
console.log('value:', value); // <-- "value: resolved value"
})