I have script1 and script2. Here is some code from script1:
var pos;
function foo(param){pos = param;}
bar(foo);
Now in script2:
function bar(callback){
//prints 'function'
console.log(typeof callback);
navigator.geolocation.getCurrentPosition(
success,
fail
);
}
function success(position, callback){
//prints 'undefined'
console.log(typeof callback);
var pos= {
lat: position.coords.latitude,
long: position.coords.longitude
};
callback(pos);
}
How do I correctly pass foo as a callback function to bar, and chain it to navigator.geolocation.getCurrentPosition? How can I correctly get pos back to script1? Why can't I just return the values instead??
successbeing called?navigator.geolocation.getCurrentPosition, I recommend using promises for what you're after. Here's an answer I submitted for this use case, earlier today: stackoverflow.com/questions/38287407/…