๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Frontend Dev/๐Ÿฅ ์ฝ”๋“œ์Šคํ…Œ์ด์ธ  FE ๋ถ€ํŠธ์บ ํ”„

Section2 Unit3 JS/Node ๋น„๋™๊ธฐ - fetchAPI

๋ฐ˜์‘ํ˜•


Section2 Unit3 [JS/Node] ๋น„๋™๊ธฐ - fetchAPI

 

๐Ÿ“Œ Chapter4. fetch API

• Fetch API๋Š” ๋„คํŠธ์›Œํฌ ํ†ต์‹ ์„ ํฌํ•จํ•œ ๋ฆฌ์†Œ์Šค ์ทจ๋“์„ ์œ„ํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•œ๋‹ค. Fetch API๋ฅผ ์‚ฌ์šฉํ•ด ์„œ๋ฒ„์— ๋„คํŠธ์›Œํฌ ์š”์ฒญ์„ ๋ณด๋‚ด๊ณ  ์ƒˆ๋กœ์šด ์ •๋ณด๋ฅผ ๋ฐ›์•„์˜ค๋Š” ์ผ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

 ๋ช‡ ๋ฒˆ ์‚ฌ์šฉํ•ด๋ด๋„ ์–ด๋ ต๋‹ค๊ณ  ์ƒ๊ฐ๋˜์—ˆ๋˜ ๋ถ€๋ถ„์ด๋ผ ์—ฌ๋Ÿฌ ์ž๋ฃŒ๋“ค๊ณผ ์ˆ˜์—…๋‚ด์šฉ์„ ๋ฐ”ํƒ•์œผ๋กœ Fetch API์™€ fetch() ๋ฉ”์†Œ๋“œ๋ฅผ ์ •๋ง ๊ฐ„๋‹จํ•˜๊ฒŒ ์ •๋ฆฌํ•ด๋ณด์•˜๋‹ค. 

 

๊ฐ„๋‹จํ•˜๊ฒŒ ์•Œ์•„๋ณด๋Š” Fetch API

๐Ÿ‘พ Fetch API ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ•„์š”ํ•  ๋•Œ ์„œ๋ฒ„์— ๋„คํŠธ์›Œํฌ ์š”์ฒญ์„ ๋ณด๋‚ด๊ณ  ์ƒˆ๋กœ์šด ์ •๋ณด๋ฅผ ๋ฐ›์•„์˜ค๋Š” ์ผ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. Fetch API๋Š” ๋„คํŠธ์›Œํฌ ํ†ต์‹ ์„ ํฌํ•จํ•œ ๋ฆฌ์†Œ์Šค ์ทจ๋“์„ ์œ„ํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ

fay-story.com

 

โญ๏ธ ๊ณผ์ œ. Part 3 - fetch API

• fetch๋ฅผ ์ด์šฉํ•ด HTTP ์š”์ฒญ์„ ๋ณด๋‚ด๊ณ , ์‘๋‹ต์„ ๋ฐ›๋Š” ์—ฐ์Šต

• chaining๊ณผ Promise.all ๊ทธ๋ฆฌ๊ณ  async/await์„ ์ด์šฉํ•ด์„œ ๊ตฌํ˜„

 

 

๐Ÿ“Œ ๊ณผ์ œ ๋ชฉํ‘œ: 2๊ฐœ์˜ ์š”์ฒญ์˜ ๊ฒฐ๊ณผ๋ฅผ ํ•˜๋‚˜์˜ ๊ฐ์ฒด๋กœ ํ•ฉ์น˜๊ธฐ

// latestNews.json

{
  "data": [
    {
      "row_id": 2,
      "title": "2021๋…„ ๊ฒฝ์ œ ์„ฑ์žฅ๋ฅ  ์ „๋ง ๋ฐ์•„",
      "source": "A์‹ ๋ฌธ",
      "timestamp": "2020/12/30"
    },
    {
      "row_id": 3,
      "title": "์ฝ”๋กœ๋‚˜19 ์ฆ๊ฐ€์ถ”์„ธ ๋Œ€ํญ ํ•˜๋ฝํ•ด",
      "source": "BBC",
      "timestamp": "2020/12/29"
    },
    {
      "row_id": 4,
      "title": "์ฝ”๋“œ์Šคํ…Œ์ด์ธ  ์ทจ์—…์—ฐ๊ณ„ ํŒŒํŠธ๋„ˆ์‚ฌ xxx๊ฑด ๋ŒํŒŒ",
      "source": "์Šคํƒ€ํŠธ์—… ๋‰ด์Šค",
      "timestamp": "2020/12/31"
    }
  ]
}
// weather.json

{
  "status": "sunny",
  "temperature": "28",
  "fineDust": "good"
}

 

๐Ÿฑ 01_basicChaining.js

// newsURL = latestNews.json
// weatherURL = weather.json

function getNewsAndWeather() {
  // TODO: fetch์„ ์ด์šฉํ•ด ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค
  // TODO: ์—ฌ๋Ÿฌ๊ฐœ์˜ Promise๋ฅผ then์œผ๋กœ ์—ฐ๊ฒฐํ•˜์—ฌ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค
  // TODO: Promise.all ๋˜๋Š” async/await์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ํ’€์–ด๋ณด์„ธ์š”
  
  const obj = {}; 
  
  return fetch(newsURL) // fetch๋กœ Response ๊ฐ์ฒด ๋ฆฌํ„ด
    .then((res) => res.json()) // Response ๊ฐ์ฒด์—์„œ JSON ํ˜•์‹์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•˜๊ณ , ์ด๋ฅผ JavaScript ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜
    .then((data) => { // data๋Š” ๋ฐ˜ํ™˜๋œ ๊ฐ์ฒด {data:Array(3)}
      obj.news = data.data; // data.data๋Š” [{..}, {..}, {..}]
      return fetch(weatherURL); // ๋‹ค์‹œ fetch๋กœ Response ๊ฐ์ฒด ๋ฆฌํ„ด
    })
    .then((res) => res.json()) // ์œ„์™€ ๊ฐ™์€ ์ž‘์—… ๋ฐ˜๋ณต
    .then((data) => {
      obj.weather = data;
      return obj;
    })
    .catch((error) => console.log(error))
}

 

๐Ÿฑ 02_promiseAll.js

// newsURL = latestNews.json
// weatherURL = weather.json

function getNewsAndWeatherAll() {
  // TODO: Promise.all์„ ์ด์šฉํ•ด ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค

  // fetch๋กœ Response ๊ฐ์ฒด๋ฅผ ๋ฐ›์•„ JSON ํ˜•์‹์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•˜๊ณ , ์ด๋ฅผ JavaScript ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜
  const news = fetch(newsURL).then((res) => res.json());
  const weather = fetch(weatherURL).then((res) => res.json());

  console.log(news) // Promise {<pending>}
  console.log(weather) // Promise {<pending>}

  return Promise.all([news, weather]) // Promise.all๋กœ ํ•œ๋ฒˆ์— ์ฒ˜๋ฆฌ
    .then((res) => { // res [{..news..}, {..weather..}]
      return { news: res[0].data, weather: res[1] }
    })
}

 

๐Ÿฑ 03_asyncAwait.js

// newsURL = latestNews.json
// weatherURL = weather.json

async function getNewsAndWeatherAsync() {
  // TODO: async/await ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•ด ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค

  // fetch๋กœ Response ๊ฐ์ฒด๋ฅผ ๋ฐ›์•„ JSON ํ˜•์‹์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•˜๊ณ , ์ด๋ฅผ JavaScript ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•˜๋Š”๋ฐ,
  // await๋ฅผ ์‚ฌ์šฉํ•ด Promise๊ฐ€ ์ฒ˜๋ฆฌ๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆผ
  const news = await fetch(newsURL).then((res) => res.json());
  const weather = await fetch(weatherURL).then((res) => res.json());

  console.log(news) // {data: Array(3)}
  console.log(weather) // {status: 'sunny', temperature: '28', fineDust: 'good'}

  return { news: news.data, weather: weather }
}

 

์ฒ˜์Œ์—” ์–ด๋ ค์›Œ์„œ ์†๋„ ๋ชป๋Œ”์—ˆ๋Š”๋ฐ, ์ดํ•ดํ•˜๊ณ  ๋‚˜๋‹ˆ ์žฌ๋ฏธ์—ˆ๋‹ค๊ณ  ์ƒ๊ฐํ•œ ๋ฌธ์ œ! (์ดํ•ดํ•˜๋ฉด ๋ญ๋“  ์žฌ๋ฐŒ๊ฒ ์ง€...)

๊ฒฐ๊ตญ์€ ์ •๋ง ๊ฐ„๋‹จํ•œ ์ตœ์‹  ๋ฌธ๋ฒ•์ธ async/await๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ์ด์œ ๊ฐ€ ์—†๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค๋˜ ๊ณผ์ œ์˜€๋‹ค. 

 


๐ŸŒ™  ์˜ค๋Š˜์˜ ํšŒ๊ณ 

 ์˜ค๋Š˜๋„ ์–ด์ œ๋ž‘ ๋น„์Šทํ–ˆ๋‹ค… ๊ณผ์ œ ํ•œ๋‹ค๊ณ  ์ •์‹ ์—†์—ˆ๊ณ , fetchAPI๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ณผ์ œ๋„ ํ—ค๋งธ๋‹ค. fetch๋กœ ์˜คํ”ˆ API๋„ ๋ช‡ ๋ฒˆ ์‚ฌ์šฉํ•ด๋ดค๋Š”๋ฐ, ๊ณผ์ œ๋กœ ๋‹ค์‹œ ํ•ด๋ณด๋ ค๋‹ˆ ์ž˜ ๋˜์ง€ ์•Š์•˜๋‹ค. ์ƒ๊ฐํ•ด๋ณด๋‹ˆ ๊ทธ ๋‹น์‹œ์— ๊ณต๋ถ€ํ•  ๋•Œ๋„ fetch์™€ promise, then ๋ฉ”์„œ๋“œ ๋“ฑ์ด ์ œ๋Œ€๋กœ ์ดํ•ด๊ฐ€ ์•ˆ๋˜์—ˆ๋Š”๋ฐ ์˜์ƒ์ด๋‚˜ ๊ตฌ๊ธ€๋ง์œผ๋กœ ์ฝ”๋“œ๋ฅผ ๋”ฐ๋ผ ์น˜๊ณ , ํ•„์š”ํ•œ ๋ถ€๋ถ„๋งŒ ์‚ฌ์šฉํ•ด์„œ ์ •ํ™•ํ•œ ์ดํ•ด๊ฐ€ ๋˜์ง€ ์•Š์•˜๋˜ ๊ฒƒ ๊ฐ™๋‹ค. ๋ณต์Šต์„ ํ•˜๋ฉด์„œ ํ™•์‹คํžˆ ์•Œ๊ณ  ๋„˜์–ด๊ฐ€์•ผ๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์–ด์„œ ์ €๋…์— ๋‹ค์‹œ ํ•œ๋ฒˆ ์ฐฌ์ฐฌํžˆ ๋ฌธ์„œ์™€ ์ˆ˜์—…์ž๋ฃŒ, ๊ณผ์ œ๋กœ ๊ตฌํ˜„ํ•œ ์ฝ”๋“œ๋ฅผ ํ•˜๋‚˜์”ฉ ๋œฏ์–ด๋ณด๋ฉฐ fetch์™€ promise, async์™€ ์ต์ˆ™ํ•ด์ง€๋ ค๊ณ  ํ–ˆ๋‹ค. ํ•œ ๋ฒˆ ๋‹ค์‹œ ๊ณต๋ถ€ํ•˜๊ณ  ๋‚˜๋‹ˆ ์ดํ•ด๊ฐ€ ๋˜๋Š”๋“ฏ ํ–ˆ๋‹ค. ์–ด๋ ค์šด ๊ฐœ๋…์€ ์•„๋‹ˆ์—ˆ๋Š”๋ฐ, ์–ด๋ ต๊ฒŒ ์ƒ๊ฐํ•˜๊ณ  ์žˆ์—ˆ๋‚˜ ๋ณด๋‹ค.

 ๋‚ด์ผ๋ถ€ํ„ฐ๋Š” ๋“œ๋””์–ด ๋ฆฌ์•กํŠธ์— ๋“ค์–ด๊ฐ„๋‹ค..!! ํ™”์ดํŒ… ๐ŸŽˆ

๋ฐ˜์‘ํ˜•