Geolocation.watchPosition()
https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/watchPosition을 약속에 넣고 async/await
관용구로 사용하는 것이 가능한지 궁금합니다. 장치의 위치가 변경되고 후속 기능을 호출 할 때마다 위치를 계속 반환합니다.Promise에서 Geolocation.watchPosition()과 같은 함수를 래핑 할 수 있습니까?
// Example Class
class Geo {
// Wrap in Promise
getCurrentPosition(options) {
return new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(resolve, reject, options)
})
}
// Wrap in Promise
watchCurrentPosition(options) {
return new Promise((resolve, reject) => {
navigator.geolocation.watchPosition(resolve, reject, options)
})
}
// Works well.
async currentPosition() {
try {
let position = await this.getCurrentPosition()
// do something with position.
}
catch (error) {
console.log(error)
}
}
// Any way...?
async watchPosition() {
try {
let position = await this.watchCurrentPosition()
// do something with position whenever location changes.
// Invoking recursively do the job but doesn't feel right.
watchPosition()
}
catch (error) {
console.log(error)
}
}
}
[이 Observables 제안]과 같은 것 (https://github.com/tc39/proposal-observable/blob/master/README.md)? – gyre
이 작업은 * 할 수 있지만 약속은 한 번 발생해야하는 작업에 이상적입니다. Observable과 같은 리스너 모델은 훨씬 더 분명합니다. – lonesomeday