3

I tried to get these variables:

Geoposition.coords.latitude
Geoposition.coords.longitude

from navigator.geolocation.getCurrentPosition by the way:

function getPosition() {
    "use strict";
    var coordinates = {
        latitude: null,
        longitude: null,
        error: false,
        testData: 'Hello, world!'
    }
    function success (pos) {
        coordinates.latitude = pos.coords.latitude;
        coordinates.longitude = pos.coords.longitude;
        coordinates.testData = 'I am John';
        console.log(coordinates); //here it is ok, latitude,longitude and testData were recorded
    }

    function fail(error){
        coordinates.error = true;
    }
    navigator.geolocation.getCurrentPosition(success, fail);
    return coordinates; // but here it is no ok: function returns "coordinates" in initial state (null, null, false, "Hello, world!")
}

As far as I know, in JavaScript objects do not clone, but here it seems like they do. What am I doing wrong? And how can I use coords out of callback "success"?

John Law
  • 417
  • 4
  • 14
  • 1
    You can not return from an asynchronous method. http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call – epascarello Oct 24 '15 at 23:55
  • So you need to break your code up into steps just like that linked question explains. – epascarello Oct 25 '15 at 00:07

1 Answers1

2

This a really common question - it is asked several times a day.
There is no easy way to turn an asynchronous operation into a synchronous one.

However you could use a ES6 feature: Promises - if you can't use ES6 you could add promises with a library like bluebird or lie

function getPosition() {
    "use strict";
    return new Promise(function(resolve, reject) {
      var coordinates = {
          latitude: null,
          longitude: null,
          error: false,
          testData: 'Hello, world!'
      }
      function success (pos) {
          coordinates.latitude = pos.coords.latitude;
          coordinates.longitude = pos.coords.longitude;
          coordinates.testData = 'I am John';
          resolve(coordinates);
      }

      function fail(error){
          coordinates.error = true;
          resolve(coordinates); // or reject(error);
      }
      navigator.geolocation.getCurrentPosition(success, fail);
    });
}

Somewhere else you can use the function synchronously:

var coordinatePromise = getPosition();

But as soon as you want to access the coordinate data you have to wait for the promise to be fullfilled:

var coordinatePromise = getPosition();
coordinatePromise.then(function(coordinates) { console.log(coordinates) }); 

See the codepen demo: http://codepen.io/anon/pen/GpxZEg

jantimon
  • 36,840
  • 23
  • 122
  • 185
  • Promises... Pretty heave stuff for replacing 1 callback, especially using a lib. Very useful to learn now though. – Rudie Oct 25 '15 at 00:31
  • 1
    Well if you use es6, babel, jquery or angular you already have promises (as you can see in the codepen demo) – jantimon Oct 25 '15 at 00:34