英文:
Value undefined after being resolved from Promise
问题
我觉得这里的问题可能是错误的等待方式,或者我在 Promise 内部有一个包装的回调函数,而我正在尝试在该回调函数内解决 Promise。不确定如何解决这个问题。非常感谢任何帮助!
async function pointInRegion(latitude, longitude) {
await new Promise(async (resolve, reject) => {
try {
const coordinate = [longitude, latitude]
const pt = turf.point(coordinate); // 尝试在这里添加 await
fs.readFile("./regions.geojson", async function (err, data) { // 尝试在这里添加 await
if (err) throw err;
const geojson = JSON.parse(data);
for (const feature of geojson.features) {
const inRegion = turf.booleanPointInPolygon(pt, feature) // 尝试在这里添加 await
if (inRegion) {
console.log(feature.properties.name)
return resolve(feature.properties.name)
}
}
})
} catch (err) {
return reject(err)
}
})
}
我在主函数中调用它:
const calculatedRegion = await pointInRegion(latitude, longitude)
console.log(calculatedRegion) // 打印 undefined
英文:
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
答案1
得分: 2
几个问题:
1. 你没有将 `Promise` 返回给任何调用者
2. 在这里不需要 `async` 或 `await`,调用函数将在 `pointInRegion` 的返回值上调用 `await`
带有注释的修改:
```javascript
function pointInRegion(latitude, longitude) {
// 这里不需要等待,调用函数会等待
// 也不需要返回 await
return new Promise((resolve, reject) => {
try {
const coordinate = [longitude, latitude]
const pt = turf.point(coordinate);
fs.readFile("./regions.geojson", function (err, data) {
if (err) {
throw err;
}
const geojson = JSON.parse(data);
for (const feature of geojson.features) {
const inRegion = turf.booleanPointInPolygon(pt, feature);
if (inRegion) {
console.log(feature.properties.name);
// 对于 resolve 不需要返回
resolve(feature.properties.name);
}
}
})
} catch (err) {
// 对于 reject 不需要返回
reject(err);
}
})
}
英文:
A couple of issues:
- You aren't returning your
Promise
to any callers - You don't need
async
orawait
here, the consuming function(s) will callawait
on the return value ofpointInRegion
Modifications with comments:
function pointInRegion(latitude, longitude) {
// no need to await here, calling functions will await
// return await is also not needed
return new Promise((resolve, reject) => {
try {
const coordinate = [longitude, latitude]
const pt = turf.point(coordinate);
fs.readFile("./regions.geojson", function (err, data) {
if (err) {
throw err;
}
const geojson = JSON.parse(data);
for (const feature of geojson.features) {
const inRegion = turf.booleanPointInPolygon(pt, feature);
if (inRegion) {
console.log (feature.properties.name);
// no return needed for resolve
resolve(feature.properties.name);
}
}
})
} catch (err) {
// no return needed for reject
reject(err);
}
})
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论