使用 Promise 在 react-native-geolocation-service react native 中从另一个文件获取位置
Getting location from another file using Promise in react native with react-native-geolocation-service
我正在尝试创建一个辅助函数来获取用户的当前位置,但我的承诺结果未定义。
这个功能正在运行,我可以检索我的坐标:
//position.js
async function getCurrentPosition() {
return new Promise((resolve, reject) => {
Geolocation.getCurrentPosition(resolve, reject, {
enableHighAccuracy: true,
timeout: 15000,
maximumAge: 10000,
});
});
}
export async function getUserLocation() {
await request(
// Check for permissions
Platform.select({
android: PERMISSIONS.ANDROID.ACCESS_COARSE_LOCATION,
ios: PERMISSIONS.IOS.LOCATION_WHEN_IN_USE,
}),
).then((res) => {
console.log('then');
// Permission OK
if (res === 'granted') {
console.log('granted');
return getCurrentPosition();
// Permission denied
} else {
console.log('Location is not enabled');
}
});
}
但是当我在这里调用我的函数时,我得到了 undefined :
import {getUserLocation} from '../../utils/position';
useEffect(() => {
getUserLocation()
.then((res) => console.log(res)) // { undefined }
.catch((err) => {
console.error(err.message);
});
}, []);
我做错了什么?
正如所写,getUserLocation() 没有 return 它的 request(...).then() 承诺。将 await
更改为 return
。
此外,您真的应该将 console.log('Location is not enabled')
更改为 throw new Error('Location is not enabled')
,从而允许 getUserLocation 的调用者看到错误(如果出现)。
export async function getUserLocation() {
return request(Platform.select({ // Check for permissions
// ^^^^^^
'android': PERMISSIONS.ANDROID.ACCESS_COARSE_LOCATION,
'ios': PERMISSIONS.IOS.LOCATION_WHEN_IN_USE
}))
.then((res) => {
if (res === 'granted') { // Permission OK
return getCurrentPosition();
} else { // Permission denied
throw new Error('Location is not enabled'); // Throwing an Error here
// makes it available to the caller
// in its catch clause.
}
});
}
我正在尝试创建一个辅助函数来获取用户的当前位置,但我的承诺结果未定义。
这个功能正在运行,我可以检索我的坐标:
//position.js
async function getCurrentPosition() {
return new Promise((resolve, reject) => {
Geolocation.getCurrentPosition(resolve, reject, {
enableHighAccuracy: true,
timeout: 15000,
maximumAge: 10000,
});
});
}
export async function getUserLocation() {
await request(
// Check for permissions
Platform.select({
android: PERMISSIONS.ANDROID.ACCESS_COARSE_LOCATION,
ios: PERMISSIONS.IOS.LOCATION_WHEN_IN_USE,
}),
).then((res) => {
console.log('then');
// Permission OK
if (res === 'granted') {
console.log('granted');
return getCurrentPosition();
// Permission denied
} else {
console.log('Location is not enabled');
}
});
}
但是当我在这里调用我的函数时,我得到了 undefined :
import {getUserLocation} from '../../utils/position';
useEffect(() => {
getUserLocation()
.then((res) => console.log(res)) // { undefined }
.catch((err) => {
console.error(err.message);
});
}, []);
我做错了什么?
正如所写,getUserLocation() 没有 return 它的 request(...).then() 承诺。将 await
更改为 return
。
此外,您真的应该将 console.log('Location is not enabled')
更改为 throw new Error('Location is not enabled')
,从而允许 getUserLocation 的调用者看到错误(如果出现)。
export async function getUserLocation() {
return request(Platform.select({ // Check for permissions
// ^^^^^^
'android': PERMISSIONS.ANDROID.ACCESS_COARSE_LOCATION,
'ios': PERMISSIONS.IOS.LOCATION_WHEN_IN_USE
}))
.then((res) => {
if (res === 'granted') { // Permission OK
return getCurrentPosition();
} else { // Permission denied
throw new Error('Location is not enabled'); // Throwing an Error here
// makes it available to the caller
// in its catch clause.
}
});
}