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
}
})
}
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
}
}
정리글 참조
출처 : https://velog.io/@khy226/%EB%8F%99%EA%B8%B0-%EB%B9%84%EB%8F%99%EA%B8%B0%EB%9E%80-Promise-asyncawait-%EA%B0%9C%EB%85%90