[英]How to use result from one async function to another
I am trying to use the latitude and longitude coordinates on an ISS api ( https://api.wheretheiss.at/v1/satellites/25544 ) in the url of another api (https://api.wheretheiss.at/v1/坐標/37.795517,-122.393693 )。 我正在嘗試使用坐標並將它們輸入 url 而不是使用硬編碼的坐標。
這是我到目前為止所做的......
https://api.wheretheiss.at/v1/coordinates/${latitude},${longitude}
筆記
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()
異步函數返回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 function,然后使用必要的數據調用getGeoLocation
getISS().then(({ latitude, longitude }) => {
getGeoLocation(latitude, longitude);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.