basicChaining.js
var newsURL = 'http://localhost:4999/data/latestNews';
var weatherURL = 'http://localhost:4999/data/weather';
function getNewsAndWeather() {
let obj = {}
return fetch(newsURL)
.then((res) => res.json())
.then((news) => {
obj.news = news.data
return fetch(weatherURL)
})
.then(res => res.json())
.then(weather => {
obj.weather = weather
return obj
})
}
promiseAll.js
var newsURL = 'http://localhost:4999/data/latestNews';
var weatherURL = 'http://localhost:4999/data/weather';
function getNewsAndWeatherAll() {
let news = fetch(newsURL)
.then(res => res.json())
let weather = fetch(weatherURL)
.then(res => res.json())
let ans = {}
return Promise.all([news, weather])
.then(res => {
ans.news = res[0].data
ans.weather = res[1]
return ans
})
}
asyncAwait.js
var newsURL = 'http://localhost:4999/data/latestNews';
var weatherURL = 'http://localhost:4999/data/weather';
async function getNewsAndWeatherAsync() {
let news = await fetch(newsURL)
.then(res => res.json())
let weather = await fetch(weatherURL)
.then(res => res.json())
let ans = {}
ans.news = news.data
ans.weather = weather
return ans
}