Promise解析后的值未定义

huangapple go评论59阅读模式
英文:

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:

  1. You aren't returning your Promise to any callers
  2. You don't need async or await here, the consuming function(s) will call await on the return value of pointInRegion

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);
    }
  })
}

huangapple
  • 本文由 发表于 2023年5月29日 01:49:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76352819.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定