1 回答

TA貢獻1835條經驗 獲得超7個贊
由于設置的原因,我無法對此進行測試,但這應該可以工作……并按照您期望的順序返回東西。
這是使用 fetch api,它通常比 xmlhttp 請求 api 干凈得多。
但是你知道,async 是包含 await 的函數的標簽。.then() 是如何在這樣的回調中排序的……等待的值將在返回等待的值之前首先執行。
async function Chart(){
let date = [], price = [], open=[], Timestamp=[], High=[], Low = [];
let selectedItem = document.getElementById('currency-selector').value;
let url = `http://127.0.0.1:8000/${selectedItem}/`;
let requestURL = url; //URL of the JSON data
return await fetch(requestURL)
.then(res=>res.json())
.then(data=>{
data.forEach(x=>{
date.push(x.date)
price.push(x.close);
High.push(x.high);
open.push(x.Open);
Low.push(x.low);
})
})
.then(()=>{
return [date,price,High,Low,open];
})
}
添加回答
舉報