Day 59: Sprint Async-and-Promise Part 3
1. TIL (Today I Learned)
Today I reviewed and tried to redo my codes I wrote in the past pair programming session. It was about how to use fetch module in javascript. It was quite tricky but I learned a lot!
Using basic Chaining
var newsURL = 'http://localhost:5000/data/latestNews';
var weatherURL = 'http://localhost:5000/data/weather';
function getNewsAndWeather() {
const result = {};
return fetch(newsURL)
.then((newsData) => newsData.json())
.then((newsJson) => {
result.news = newsJson.data
return fetch(weatherURL)
})
.then((weatherData) => weatherData.json())
.then((weatherJson) => {
result.weather = weatherJson
return result;
})
// TODO: fetch을 이용해 작성합니다
// TODO: 여러개의 Promise를 then으로 연결하여 작성합니다
}
if (typeof window === 'undefined') {
module.exports = {
getNewsAndWeather
}
}
Using Promise.all()
var newsURL = 'http://localhost:5000/data/latestNews';
var weatherURL = 'http://localhost:5000/data/weather';
function getNewsAndWeatherAll() {
return Promise.all([fetch(newsURL), fetch(weatherURL)])
.then(([news, weather]) => Promise.all([news.json(), weather.json()]))
.then(([news, weather]) => ({news: news.data, weather}))
}
// return Promise.all([fetch(newsURL), fetch(weatherURL)])
// .then(([news, weather]) => Promise.all([news.json(), weather.json()]))
// .then(([news, weather]) => ({news: news.data, weather}))
if (typeof window === 'undefined') {
module.exports = {
getNewsAndWeatherAll
}
}
Using asyncAwait
var newsURL = 'http://localhost:5000/data/latestNews';
var weatherURL = 'http://localhost:5000/data/weather';
async function getNewsAndWeatherAsync() {
const news = await fetch(newsURL).then((news) => news.json());
const weather = await fetch(weatherURL).then((weather) => weather.json());
return { news: news.data, weather: weather }
}
// const news = await fetch(newsURL).then((data1) => data1.json());
// const weather = await fetch(weatherURL).then((data2) => data2.json());
// return { news: news.data, weather: weather };
if (typeof window === 'undefined') {
module.exports = {
getNewsAndWeatherAsync
}
}
2. 3 Things to be Thankful For
- Thankful for God reminding me how weak I am and arrogant I am.
- Thankful for God allowing me to have a interview.
- Thankful for God reminding me of His Love.
3. Ideas and Things to Think About
- Don't try to invent the wheel, is a quote my teacher told me during the live session. I got inspired on reminding myself that the best way to start a bootcamp in Thailand is to use the data base codestates have and move it into Thailand and translate it. Don't try to reinvent the wheel!
Author And Source
이 문제에 관하여(Day 59: Sprint Async-and-Promise Part 3), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://velog.io/@inseoparkk/Day-59-Sprint-Async-and-PromisePart-3
저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
- Don't try to invent the wheel, is a quote my teacher told me during the live session. I got inspired on reminding myself that the best way to start a bootcamp in Thailand is to use the data base codestates have and move it into Thailand and translate it. Don't try to reinvent the wheel!
Author And Source
이 문제에 관하여(Day 59: Sprint Async-and-Promise Part 3), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@inseoparkk/Day-59-Sprint-Async-and-PromisePart-3저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)