JavaScript:为什么这个承诺没有返回值? [重复]

问题描述 投票:0回答:1

我被一个应该从 API 获取数据然后返回预定义类型的数组的函数所困扰。但是,我总是会收到一条错误消息,表明

A function whose declared type is neither 'void' nor 'any' must return a value.

我真的不明白为什么会出现此消息,因为我的函数中有一个返回语句,即使在获取数据时发生错误:

export async function queryOpenCage(query: string): Promise<MarkerPoint[]> {
  const {opencageApiKey}: ClientSettings = JSON.parse(
    document.getElementById(ElementID.Settings)!.textContent!
  )
  const fetchURL = `https://api.opencagedata.com/geocode/v1/json?key=${opencageApiKey}&q=${query}&limit=5&pretty=1`
  const resultItems: MarkerPoint[] = []
  fetch(fetchURL)
    .then(response => {
      return response.json()
    })
    .then(json => {
      const items = json.results.map((res: any) => {
        return {
          lat: res.geometry.lat,
          lng: res.geometry.lng,
          address: res.formatted
        }
      })
      return resultItems.push(...items)
    })
    .catch(error => {
      return []
    })
}

非常感谢任何帮助或建议!

javascript fetch-api es6-promise
1个回答
0
投票
fetch(fetchURL)

应该是

return fetch(fetchURL)
© www.soinside.com 2019 - 2024. All rights reserved.