fetchを使った非同期通信
const url = "http://api.open-notify.org/astros.json";
function getJson(url) {
return fetch(url).then((response) => {
return new Promise((resolve, reject) => {
response.json()
.then((data) => resolve(data))
.catch((error) => reject(error));
});
});
}
getJson(url)
.then((data) => {
console.log(data.people);
})
.catch((error) => {
console.log(new Error("Can not load!!"));
});
fetch("https://jsonplaceholder.typicode.com/users")
.then(response => response.json())
.then(data => {
for (const { id, name } of data) {
console.log(id, name)
};
});
async function myFetch() {
const response = await fetch("https://jsonplaceholder.typicode.com/users");
const data = await response.json();
for ({ email, phone } of data) {
console.log(email, phone);
}
}
myFetch();