I have this module that wraps a call to an asynchronous method to watch for changes in the browser's geolocation navigator.geolocation.watchPosition
var geo_value;
//Initialises the devices geolocation
navigator.geolocation.watchPosition(function (position) {
geo_value = position.coords.longitude + "," + position.coords.latitude;
});
Everytime the watchPosition method call fires, I store the value in a variable to provide me programmatic access to the currentPosition at any given time.
This all works fine, however I now want to expose this currentPosition via a custom async method that I am exporting from my module.
define(["global"], function (window) {
var navigator = window.navigator;
var geo_value;
//Initialises the devices geolocation
navigator.geolocation.watchPosition(function (position) {
geo_value = position.coords.longitude + "," + position.coords.latitude;
});
return {
currentPosition: function (callback) {
setTimeout(function () {
while (!geo_value) {
//keep looping until geo_value has been initially set by the watch position above
}
//geo_value has been set, execute callback
callback(geo_value);
}, 0);
}
};
});
I have made the module expose the currentPosition, which works if geo_value has already been set, but I also want it to wait asynchronously if geo_value hasn't been set and then execute the callback when the watchPosition finally sets a value for geo_value. I tried using the setTimeout function to attempt at making it asynchronous, but it didn't work.
Please any help will be much appreciated.