javascript - JS - Promise + geolocation.watchPosition -
i'm trying use javascript promise geolocation, can't make work correctly geolocation.watchposition, then clause being called once :
function geolocation() { this._options = { enablehighaccuracy: true, maximumage : 10000, timeout : 7000 } } geolocation.prototype = { watchid() { return this._watchid; }, set watchid(watchid) { this._watchid = watchid; }, options() { return this._options; }, // hascapability: function() { return "geolocation" in navigator; }, _promise: function(promise) { var geolocation = this; if (promise == "getposition") return new promise(function(ok, err) { navigator.geolocation.getcurrentposition( ok.bind(geolocation), err.bind(geolocation), geolocation.options ); }); else if (promise == "watchposition") return new promise(function(ok, err) { geolocation.watchid = navigator.geolocation.watchposition( ok.bind(geolocation), err.bind(geolocation), geolocation.options ); }); }, getposition: function() { return this._promise('getposition'); }, watchposition: function() { this.clearwatch(); return this._promise('watchposition'); }, clearwatch: function() { if (!this.watchid) return; navigator.geolocation.clearwatch(this.watchid); this.watchid = null; } }; var geolocation = new geolocation(); geolocation.watchposition() .then( function(position) { console.log("latitude: " + position.coords.latitude + " - longitude: " + position.coords.longitude) }, function(error) { console.log("error: " + error); } )
i tried intermediary promise returned watchposition/0, returns same result.
what missing ?
answering myself.
following @benjamin gruenbaum advice on using callback, it's possible combine single callback handling both geolocation.watchposition
responses promise, , use then().catch()
pattern (below in notify
function) :
function geolocation() { this._options = { enablehighaccuracy: true, maximumage : 10000, timeout : 7000 } }; geolocation.prototype = { watchid() { return this._watchid; }, set watchid(watchid) { this._watchid = watchid; }, options() { return this._options; }, // hascapability: function() { return "geolocation" in navigator; }, _promise: function(promise, cb) { var geolocation = this; return new promise(function(ok, err) { if (promise == "getposition") navigator.geolocation.getcurrentposition(cb, cb, geolocation.options ); else if (promise == "watchposition") geolocation.watchid = navigator.geolocation.watchposition( cb, cb, geolocation.options ); }); }, getposition: function(cb) { return this._promise("getposition", cb); }, watchposition: function(cb) { this.clearwatch(); return this._promise("watchposition", cb); }, clearwatch: function() { if (!this.watchid) return; navigator.geolocation.clearwatch(this.watchid); this.watchid = null; } }; /* testing functions module */ function log(data) { console.log(date() + " " + data); }; function logok({coords: {latitude: latitude, longitude: longitude}}) { log("latitude: " + latitude + " - longitude: " + longitude); }; function logerror({code: code, message: message}) { log("error geo " + code + " - " + message); }; /* callback module */ function notify(event) { return new promise( function(ok, err) { event.coords ? ok(event) : err(event); } ).then(logok).catch(logerror); }; /**/ var geolocation = new geolocation(); // geolocation.getposition(notify); geolocation.watchposition(notify);
not sure if use promise correctly, works , allows take advantage of chaining.
Comments
Post a Comment