プロンプトでGeolocation.watchPosition()
https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/watchPositionをラップして、それが仕事のやり方でasync/await
のイディオムで使用できるかどうかは疑問です。デバイスの場所が変更されたときに常に位置を返し、後続の関数を呼び出します。Geolocation.watchPosition()などの関数をPromiseでラップすることはできますか?
// 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)
}
}
}
[このオブザーバブル提案](https://github.com/tc39/proposal-observable/blob/master/README.md)のようなものですか? – gyre
これは*できますが、約束は一度起こる必要があることに理想的です。 Observableのようなリスナーモデルはもっとはっきりしています。 – lonesomeday