async (異步) function – start
JavaScript
const start = async() => {
//當回傳了資料 才執行 console.log
let received_data = await getData();
console.log("取得的資料:", received_data);
}
async (異步) function – getData
JavaScript
const getData = async() => {
//等待伺服器回應
let response = await fetch('url');
//當伺服器回應了才取得資料
let data = response.json();
return data;
}
調用方法
JavaScript
window.onload = () => {
start();
}