Skip to main content
added 1 character in body
Source Link
Pavlo
  • 418
  • 2
  • 9

Most native functions require callback instead of Promise because they support older browsers, so what you have to do is to pack the method in a Promise

function getLocation() {
    return new Promise(function(resolve, reject){ 
        navigator.geolocation.getCurrentPosition(
            function(position) { resolve(position); },
            function(error) { reject(error); }
        }
    });
}

Now are able to determine what should happen with the position and the error.

const response = getLocation()
    .then(position => 
        ( {"latitude": position.coords.latitude, 
           "longitude": position.coords.longitude} ))
    .then(latLong => { unlock_request(latLong) }, 
          error => { get_location_denied(error[1]) });

You know that for this request that you want to get the latitude and longitude out of position so you just return a new object with longitude and latitude, the smart thing with promises is that is it a builder pattern, so can call then again and now the parameter will be what was returned in the last then, so all power is given to reponseresponse rather than getLocation()

Most native functions require callback instead of Promise because they support older browsers, so what you have to do is to pack the method in a Promise

function getLocation() {
    return new Promise(function(resolve, reject){ 
        navigator.geolocation.getCurrentPosition(
            function(position) { resolve(position); },
            function(error) { reject(error); }
        }
    });
}

Now are able to determine what should happen with the position and the error.

const response = getLocation()
    .then(position => 
        ( {"latitude": position.coords.latitude, 
           "longitude": position.coords.longitude} ))
    .then(latLong => { unlock_request(latLong) }, 
          error => { get_location_denied(error[1]) });

You know that for this request that you want to get the latitude and longitude out of position so you just return a new object with longitude and latitude, the smart thing with promises is that is it a builder pattern, so can call then again and now the parameter will be what was returned in the last then, so all power is given to reponse rather than getLocation()

Most native functions require callback instead of Promise because they support older browsers, so what you have to do is to pack the method in a Promise

function getLocation() {
    return new Promise(function(resolve, reject){ 
        navigator.geolocation.getCurrentPosition(
            function(position) { resolve(position); },
            function(error) { reject(error); }
        }
    });
}

Now are able to determine what should happen with the position and the error.

const response = getLocation()
    .then(position => 
        ( {"latitude": position.coords.latitude, 
           "longitude": position.coords.longitude} ))
    .then(latLong => { unlock_request(latLong) }, 
          error => { get_location_denied(error[1]) });

You know that for this request that you want to get the latitude and longitude out of position so you just return a new object with longitude and latitude, the smart thing with promises is that is it a builder pattern, so can call then again and now the parameter will be what was returned in the last then, so all power is given to response rather than getLocation()

Source Link
Pavlo
  • 418
  • 2
  • 9

Most native functions require callback instead of Promise because they support older browsers, so what you have to do is to pack the method in a Promise

function getLocation() {
    return new Promise(function(resolve, reject){ 
        navigator.geolocation.getCurrentPosition(
            function(position) { resolve(position); },
            function(error) { reject(error); }
        }
    });
}

Now are able to determine what should happen with the position and the error.

const response = getLocation()
    .then(position => 
        ( {"latitude": position.coords.latitude, 
           "longitude": position.coords.longitude} ))
    .then(latLong => { unlock_request(latLong) }, 
          error => { get_location_denied(error[1]) });

You know that for this request that you want to get the latitude and longitude out of position so you just return a new object with longitude and latitude, the smart thing with promises is that is it a builder pattern, so can call then again and now the parameter will be what was returned in the last then, so all power is given to reponse rather than getLocation()