我试图在另一个 api ( https://api.wheretheiss.at/v1/satellites/25544 ) 的 URL 中使用 ISS api ( https://api.wheretheiss.at/v1/coordinates/37.795517,-122.393693 ) 上的纬度和经度坐标。我正在尝试使用坐标并将其输入到网址中,而不是使用硬编码的坐标。
这就是我到目前为止所做的...
- 我尝试使用模板字符串使坐标动态化:
https://api.wheretheiss.at/v1/coordinates/${latitude},${longitude}
- 我制作了两个单独的异步等待函数:(1) getISS() 获取纬度/经度坐标,(2) getGeoLocation() 获取这些坐标并获取country_code/timecode_id 数据
- 我还尝试过以纬度、经度作为参数调用 getGeoLocation() 并将它们传递给参数的纬度和经度,但这只会导致 500 错误
注意
- 我知道这两个 url api 的工作原理,因为我已经测试了它们并收到了我正在寻找的数据
- 这是API网站https://wheretheiss.at/w/developer
const api_url_id = 'https://api.wheretheiss.at/v1/satellites/25544'
//async await getISS function
async function getISS() {
const response = await fetch(api_url_id)
const data = await response.json()
const {
latitude,
longitude,
velocity,
visibility
} = data
}
async function getGeoLocation(latitude, longitude) {
const response2 = await fetch(`https://api.wheretheiss.at/v1/coordinates/${latitude},${longitude}`)
const data2 = await response2.json()
const {
timezone_id,
country_code
} = data2
console.log(data2.timezone_id,country_code)
}
getGeoLocation(data.latitude, data.longitude)
getISS()
最佳答案
async 函数返回一个 promise,因此您可以使用 .then()
您应该从getISS返回数据并使用.then(),就像这样......
// getISS function returns data
async function getISS() {
const response = await fetch(api_url_id);
const data = await response.json();
return data;
}
调用您的 getISS 函数,使用 then 稍后使用必要的数据调用 getGeoLocation
getISS().then(({ latitude, longitude }) => {
getGeoLocation(latitude, longitude);
});
关于javascript - 如何将一个异步函数的结果使用到另一个异步函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58574008/