I think the problem here might be wrong awaits, or the fact that I have a wrapped up callback inside the promise and I'm trying to resolve the promise inside that callback. Not sure how to fix this. Any help is much appreciated!
async function pointInRegion(latitude, longitude) {
await new Promise(async (resolve, reject) => {
try {
const coordinate = [longitude, latitude]
const pt = turf.point(coordinate); // tried adding await here
fs.readFile("./regions.geojson", async function (err, data) { // tried adding await here
if (err) throw err;
const geojson = JSON.parse(data);
for (const feature of geojson.features) {
const inRegion = turf.booleanPointInPolygon(pt, feature) // tried adding await here
if (inRegion) {
console.log (feature.properties.name)
return resolve(feature.properties.name)
}
}
})
} catch (err) {
return reject(err)
}
})
}
I call it in the main function like:
const calculatedRegion = await pointInRegion(latitude, longitude)
console.log(calculatedRegion) // prints undefined