fetch API .json 메소드 = JSON.parse 와 같다. https://wooooooak.github.io/javascript/2018/11/25/fetch&json()/
관련사이트에 접속에서 내용을 꼭 확인해 보자
const newsURL = 'http://localhost:5000/data/latestNews';
const weatherURL = 'http://localhost:5000/data/weather';
function getNewsAndWeather() {
return fetch(newsURL)
.then((resp) => resp.json()) // .json
.then((json1) => {
return fetch(weatherURL)
.then((resp) => resp.json())
.then((json2) => {
return {
news: json1.data,
weather: json2,
};
});
});
}
// function getNewsAndWeather() {
// const obj = {}
// return fetch(newsURL)
// .then(data => data.json())
// .then(newsArg =>{
// obj["news"] = newsArg.data
// return fetch(weatherURL)
// })
// .then(wtResponse => wtResponse.json())
// .then(wtArg => {
// obj["weather"] = wtArg
// return obj;
// })
// // TODO: fetch을 이용해 작성합니다
// // TODO: 여러개의 Promise를 then으로 연결하여 작성합니다
// }
if (typeof window === 'undefined') {
module.exports = {
getNewsAndWeather
}
}
var newsURL = 'http://localhost:5000/data/latestNews';
var weatherURL = 'http://localhost:5000/data/weather';
function getNewsAndWeatherAll() {
return Promise.all([fetch(newsURL), fetch(weatherURL)])
.then(([newsResponse, weatherResponse]) => {
return Promise.all([newsResponse.json(), weatherResponse.json()]);
})
.then(([json1, json2]) => {
return {
news: json1.data,
weather: json2,
};
});
}
// function getNewsAndWeatherAll() {
// const obj= {};
// return Promise.all([fetch(newsURL).then(data => data.json()),fetch(weatherURL).then(wtData => wtData.json())])
// .then((response) => {
// obj.news = response[0].data
// obj.weather = response[1]
// return obj;
// })
// // TODO: Promise.all을 이용해 작성합니다
// }
if (typeof window === 'undefined') {
module.exports = {
getNewsAndWeatherAll
}
}
var newsURL = 'http://localhost:5000/data/latestNews';
var weatherURL = 'http://localhost:5000/data/weather';
async function getNewsAndWeatherAsync() {
let obj = {}
const newsData = await fetch(newsURL).then(data => data.json())
const weatherData = await fetch(weatherURL).then(wtData => wtData.json())
obj["news"] = newsData.data
obj["weather"] = weatherData
return obj;
// TODO: async/await 키워드를 이용해 작성합니다
}
if (typeof window === 'undefined') {
module.exports = {
getNewsAndWeatherAsync
}
}