I am building a react native app and have run into an issue with my promises. This code was working before, but now for some reason one of my Promises is resolving "undefined" on its own without going through its execution... I have included code to represent the flow of my App component, the logic begins in componentDidMount()
I have re-coded using traditional promises and .then and am having the same issue.
async componentDidMount() {
console.log('call 1');
let location = await this.getPermissions();
console.log('finish async');
console.log(location);
}
getPermissions = async () => {
console.log('start 1');
// Other logic here to determine platform, resulting in the next call
console.log('call 2');
let location = await this.getPosition();
console.log('respond 1');
console.log(location);
return location;
}
getPosition = async () => {
console.log('start 2');
// Promise resolves here with "undefined" before the
// getCurrentPosition finishes executing
navigator.geolocation.getCurrentPosition(
position => {
// Logic to find closest
console.log('respond 2');
console.log(closest);
return closest;
}
);
}
So with the log statements, the proper flow should be - call 1 - start 1 - call 2 - start 2 - respond 2 - result - respond 1 - result - finish async - result
The output I am getting however is - call 1 - start 1 - call 2 - start 2 - respond 1 - undefined - finish async - undefined - respond 2 - result ---- this result IS correct, but promises are prematurely resolving undefined