问题描述
我试图使这段代码同步,但是由于某种原因async/await无法正常工作.我在React-native中使用两个不同的模块工作.我想在googleMaps中查看我的地理位置,但是因为异步的东西,它给我一个错误.
I'm trying to make this code to be synchronous but for some reason async/await doesn't work.. Im working in React-native with two differents modules. I want to see my geolocation in googleMaps but it gets me a error because the asynchronous stuff.
应用程序是根组件,它导入了getLocalitation函数.
App is the root component, im importing getLocalitation function.
export default class App extends Component {
Appmetod = async () => {
const resp = await getLocalitation();
console.log('Appmetod: latitud: ' + resp.latitude);
Linking.openURL(`http://www.google.com/maps/place/-33.317597,-71.405500`);
}
render() {
return (
<View style={styles.container}>
<Button title="Click me" onPress={this.Appmetod } />
</View>
);
}
}
const getLocalitation = () =>{
console.log('DENTRO DE GetLocalitaion');
const geoOptions={
enableHighAccuracy: true,
timeOut: 10000
};
const coordenates = navigator.geolocation.getCurrentPosition( geoSucces,goFailure, geoOptions);
console.log('DESPUES DE COORDENATES');
return coordenates;
}
const geoSucces = (position) => {
console.log('DENTRO DE GEOSUCCEES');
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
const coordenates={
latitude: latitude,
longitude: longitude
};
console.log('COORDENATES: ' + coordenates.latitude);
return coordenates;
}
const goFailure = (err) => {
console.log('Error en al geolocalizar: ' + err);
return null;
}
输出:
C:\Users\jnunez\React-Native\Proyects\geolocation\node_modules\react-native\Libraries\Utilities\infoLog.js:16 Running application "geolocation" with appParams: {"rootTag":161}. __DEV__ === true, development-level warning are ON, performance optimizations are OFF
C:\Users\jnunez\React-Native\Proyects\geolocation\src\getLocalitation.js:2 DENTRO DE GetLocalitaion
C:\Users\jnunez\React-Native\Proyects\geolocation\src\getLocalitation.js:10 DESPUES DE COORDENATES
C:\Users\jnunez\React-Native\Proyects\geolocation\src\getLocalitation.js:16 DENTRO DE GEOSUCCEES
C:\Users\jnunez\React-Native\Proyects\geolocation\src\getLocalitation.js:26 COORDENATES: -32.92098393
C:\Users\jnunez\React-Native\Proyects\geolocation\node_modules\react-native\Libraries\YellowBox\YellowBox.js:67 Possible Unhandled Promise Rejection (id: 0):
TypeError: Cannot read property 'latitude' of undefined
TypeError: Cannot read property 'latitude' of undefined
at _callee$ (blob:http://localhost:8081/19d9ce97-42d2-4939-91b8-160b264c9c79:1895:58)
at tryCatch (blob:http://localhost:8081/19d9ce97-42d2-4939-91b8-160b264c9c79:41538:19)
at Generator.invoke [as _invoke] (blob:http://localhost:8081/19d9ce97-42d2-4939-91b8-160b264c9c79:41713:24)
at Generator.prototype.<computed> [as next] (blob:http://localhost:8081/19d9ce97-42d2-4939-91b8-160b264c9c79:41581:23)
at tryCatch (blob:http://localhost:8081/19d9ce97-42d2-4939-91b8-160b264c9c79:41538:19)
at invoke (blob:http://localhost:8081/19d9ce97-42d2-4939-91b8-160b264c9c79:41614:22)
at blob:http://localhost:8081/19d9ce97-42d2-4939-91b8-160b264c9c79:41624:15
at tryCallOne (blob:http://localhost:8081/19d9ce97-42d2-4939-91b8-160b264c9c79:45254:14)
at blob:http://localhost:8081/19d9ce97-42d2-4939-91b8-160b264c9c79:45355:17
at blob:http://localhost:8081/19d9ce97-42d2-4939-91b8-160b264c9c79:46233:21
console.warn @ C:\Users\jnunez\React-Native\Proyects\geolocation\node_modules\react-native\Libraries\YellowBox\YellowBox.js:67
onUnhandled @ C:\Users\jnunez\React-Native\Proyects\geolocation\node_modules\react-native\Libraries\Promise.js:45
onUnhandled @ C:\Users\jnunez\React-Native\Proyects\geolocation\node_modules\promise\setimmediate\rejection-tracking.js:71
(anonymous) @ C:\Users\jnunez\React-Native\Proyects\geolocation\node_modules\react-native\Libraries\Core\Timers\JSTimers.js:256
_callTimer @ C:\Users\jnunez\React-Native\Proyects\geolocation\node_modules\react-native\Libraries\Core\Timers\JSTimers.js:152
callTimers @ C:\Users\jnunez\React-Native\Proyects\geolocation\node_modules\react-native\Libraries\Core\Timers\JSTimers.js:414
__callFunction @ C:\Users\jnunez\React-Native\Proyects\geolocation\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:366
(anonymous) @ C:\Users\jnunez\React-Native\Proyects\geolocation\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:106
__guard @ C:\Users\jnunez\React-Native\Proyects\geolocation\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:314
callFunctionReturnFlushedQueue @ C:\Users\jnunez\React-Native\Proyects\geolocation\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:105
(anonymous) @ debuggerWorker.js:80
推荐答案
await
/ async
不会阻止代码异步.
await
/ async
do not stop code being asynchronous.
它们是使您可以通过管理承诺来编写非异步样式代码的工具.
They are tools which let you write non-asynchronous style code by managing promises.
您只能等待
一个承诺. getLocalitation
不返回承诺.
You can only await
a promise. getLocalitation
does not return a promise.
请参见如何转换现有的回调API达成承诺?获得对 navigator.geolocation.getCurrentPosition
的承诺.
这篇关于如何使此功能与Asyn/await同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!