I need to return a promise which contains an object. I have tried this but I'm not sure if this is the correct way?
export const getCurrentLocation = async () => {
const currentLoc = {
code: null,
position: null,
watchId: null,
};
const promise = new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(
(position) => {
currentLoc.code = 200;
currentLoc.position = position;
resolve(currentLoc);
},
(error) => {
console.log('Postion==err===>', error);
currentLoc.code = 400;
reject(currentLoc);
},
{enableHighAccuracy: true, timeout: 20000, maximumAge: 1000},
);
}).then(
new Promise((resolve, reject) => {
const watchId = navigator.geolocation.watchPosition();
currentLoc.watchId = watchId;
resolve(currentLoc);
}),
);
return promise;
};
Inside Promise block I have asynchronous function