I create app with redux saga and I have problem with geolocation. Actually I found the solution but I don't understand how it works.
function userPositionPromised() {
const position = {}
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition (
location => position.on({location}),
error => position.on({error}),
{ enableHighAccuracy: true }
)
}
return { getLocation: () => new Promise(location => position.on = location) }
}
function* getUserLocation() {
yield put({type: GET_LOCATION_REQUESTED});
const { getLocation } = yield call(userPositionPromised)
const { error, location } = yield call(getLocation)
if (error) {
console.log('Failed to get user position!', error)
const { message, code } = error;
yield put({type: GET_LOCATION_FAILED, payload: { code, message }});
} else {
console.log('Received User Location', location)
const { latitude: lat, longitude: lng } = location.coords;
yield put({type: GET_LOCATION_SUCCESS, payload: { lat, lng } });
}
}
I understand getUserLocation but when it comes to userPositionPromised I don't get it. Especially this part:
location => position.on({location}),
error => position.on({error}),
and
return { getLocation: () => new Promise(location => position.on = location) }