var newsURL = 'http://localhost:4999/data/latestNews';
var weatherURL = 'http://localhost:4999/data/weather';
function getNewsAndWeather() {
return fetch(newsURL)
.then(resp => resp.json())
.then(json1 => {
return fetch(weatherURL)
.then(resp => resp.json())
.then(json2 => {
return {
news: json1.data,
weather: json2
}
});
})
}
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
}
})
}
RawBlame
async function getNewsAndWeatherAsync() {
let json1 = await fetch(newsURL).then(resp => resp.json());
let json2 = await fetch(weatherURL).then(resp => resp.json());
return {
news: json1.data,
weather: json2
}
}
헷갈린점
1. 링크주소는 html script에 있으므로 모두 연결이 된다
2. fetch의 response도 임의라서 바꿔쓸수 잇는 것 같다.
3. news의 data가 있는줄 몰랐다 꼭 콘솔창에 찍어보자