비동기_fetch API

김범주·2022년 3월 11일
0

Section 2

목록 보기
8/14

fetch API

fetch(url)
  .then((response) => console.log("response:", response))
  .catch((error) => console.log("error:", error));

URL을 인자로 받아서 Promise 객체를 반환함
스프린트에서 사용한 경우는 대부분 이런 형식 (fetch - promise 객체 - json - 사용)

function getNewsAndWeather() {
  // TODO: fetch을 이용해 작성합니다
  // TODO: 여러개의 Promise를 then으로 연결하여 작성합니다 
  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
  })
}
profile
개발꿈나무

0개의 댓글