[miniProjects] 10_Dad Jokes

๋ณด๋ฆฌยท2023๋…„ 6์›” 16์ผ
0

miniProjects

๋ชฉ๋ก ๋ณด๊ธฐ
11/47

10_Dad Jokes

๐Ÿ’ป ์ฃผ์ œ : Fetch API๋ฅผ ํ†ตํ•ด ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋ฌด์ž‘์œ„๋กœ ์•„์žฌ๊ฐœ๊ทธ๊ฐ€ ๋‚˜์˜ด.

  • async & await ๋น„๋™๊ธฐ์ฒ˜๋ฆฌ
async function generateJoke() {
  const config = {
    headers: {
      'Accept': 'application/json'
    },
  }

  const res = await fetch('https://icanhazdadjoke.com', config);
  const data = await res.json();
  jokeEl.innerHTML = data.joke;
}

โœ”๏ธ other ver.

function generateJoke() {
  const config = {
    headers: {
      'Accept': 'application/json'
    },
  }


  fetch('https://icanhazdadjoke.com', config)
    .then(res => res.json())
    .then((data) => {
      jokeEl.innerHTML = data.joke;
    });
}

โ—๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ

  • ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋Š” JavaScript์—์„œ ์žฅ๊ธฐ ์‹คํ–‰ ์ž‘์—…์ด๋‚˜ ๋„คํŠธ์›Œํฌ ์š”์ฒญ๊ณผ ๊ฐ™์€ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•  ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค. ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ํ†ตํ•ด ์žฅ๊ธฐ ์‹คํ–‰ ์ž‘์—…์ด ์ง„ํ–‰๋˜๋Š” ๋™์•ˆ ๋‹ค๋ฅธ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ž‘์—…์ด ์™„๋ฃŒ๋˜๋ฉด ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.
๐Ÿ˜ถasync ํ‚ค์›Œ๋“œ๋Š” ํ•จ์ˆ˜ ์ •์˜ ์•ž์— ์‚ฌ์šฉ๋˜๋ฉฐ, ํ•ด๋‹น ํ•จ์ˆ˜๊ฐ€ ๋น„๋™๊ธฐ ํ•จ์ˆ˜์ž„์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.
๐Ÿ˜ถawait ํ‚ค์›Œ๋“œ๋Š” async ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉ๋˜๋ฉฐ, ๋น„๋™๊ธฐ ์ž‘์—…์ด ์™„๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.
  • async ํ•จ์ˆ˜๋Š” ํ•ญ์ƒ Promise๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. await ํ‚ค์›Œ๋“œ๋Š” Promise๋ฅผ ๋Œ€๊ธฐํ•˜๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•จ์œผ๋กœ์จ ๋น„๋™๊ธฐ ์ž‘์—…์˜ ๊ฒฐ๊ณผ๋ฅผ ๋™๊ธฐ์ ์ธ ๋ฐฉ์‹์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

[์˜ˆ์ œ ์ฝ”๋“œ]

async function fetchData() {
  try {
    const response = await fetch('https://api.example.com/data'); // ๋น„๋™๊ธฐ ์ž‘์—…
    const data = await response.json(); // ๋น„๋™๊ธฐ ์ž‘์—…
    console.log(data);
  } catch (error) {
    console.log('Error:', error);
  }
}

fetchData();
  • fetchData ํ•จ์ˆ˜๋Š” async ํ•จ์ˆ˜.
  • await ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ fetch ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ณ , ํ•ด๋‹น ๋น„๋™๊ธฐ ์ž‘์—…์ด ์™„๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฐ๋‹ค. ๊ทธ ํ›„, await ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ response.json()์„ ํ˜ธ์ถœํ•˜๊ณ , ๋‹ค์‹œ ๋น„๋™๊ธฐ ์ž‘์—…์ด ์™„๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฐ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ๋ฅผ data ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๊ณ  ์ถœ๋ ฅํ•œ๋‹ค.
  • async ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์˜ค๋ฅ˜๋Š” try...catch ๋ฌธ์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๋น„๋™๊ธฐ ์ž‘์—… ์ค‘ ๋ฐœ์ƒํ•˜๋Š” ์˜ˆ์™ธ๋ฅผ ์บ์น˜ํ•˜๊ณ  ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • await ํ‚ค์›Œ๋“œ๋Š” Promise ๊ฐ์ฒด ์™ธ์—๋„ async ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ awaitableํ•œ ๊ฐ’(์˜ˆ: ๋‹ค๋ฅธ async ํ•จ์ˆ˜, Promise๊ฐ€ ์•„๋‹Œ ๊ฐ’ ๋“ฑ)์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ๊ฒฝ์šฐ, await๋Š” ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ, ํ•ด๋‹น ๊ฐ’์€ ๋น„๋™๊ธฐ ์ž‘์—…์˜ ์™„๋ฃŒ ์—ฌ๋ถ€์™€ ๊ด€๊ณ„์—†์ด ์‚ฌ์šฉ๋œ๋‹ค.
    ๐Ÿ‘๐Ÿป async์™€ await๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋น„๋™๊ธฐ ์ž‘์—…์„ ๋ณด๋‹ค ์ง๊ด€์ ์ด๊ณ  ๋™๊ธฐ์ ์ธ ์ฝ”๋“œ์™€ ์œ ์‚ฌํ•œ ๊ตฌ๋ฌธ์œผ๋กœ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๋น„๋™๊ธฐ ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ๊ณผ ์œ ์ง€๋ณด์ˆ˜์„ฑ์„ ํ–ฅ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.
profile
์ •์‹ ์ฐจ๋ ค ์ด ๊ฐ๋ฐ•ํ•œ ์„ธ์ƒ์†์—์„œ

0๊ฐœ์˜ ๋Œ“๊ธ€