[JS] promise, fetch

Chenยท2024๋…„ 6์›” 6์ผ

Javascript

๋ชฉ๋ก ๋ณด๊ธฐ
7/22
post-thumbnail

๋…ธ๋งˆ๋“œ JS ๊ฐ•์˜์—์„œ weather API๋ฅผ ๋‹ค๋ฃจ๋‹ค๊ฐ€ ๋“ฑ์žฅํ•œ fetch์™€ promise
๋ฆฌ์•กํŠธ์—์„œ๋„ ์ผ์—ˆ๋Š”๋ฐ ์›๋ฆฌ๋„ ๋ชจ๋ฅด๊ณ  ์ผ๋˜ ๊ธฐ์–ต์ด


promise ๊ฐ์ฒด

๋™๊ธฐ์ฒ˜๋Ÿผ ์ฒ˜๋ฆฌ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋ฏธ๋ž˜์˜ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๊ฐ€ ์™„๋ฃŒ๋œ ์‹œ์ ์— ๊ฒฐ๊ณผ๋ฅผ ์ œ๊ณตํ•˜๊ฒ ๋‹ค๋Š” โ€˜์•ฝ์†(ํ”„๋กœ๋ฏธ์Šค)โ€™๋ฅผ ๋ฐ˜ํ™˜
๋‹น์žฅ ๋ญ”๊ฐ€ ์ผ์–ด๋‚˜์ง€ ์•Š๊ณ , ์‹œ๊ฐ„์ด ์ข€ ๊ฑธ๋ฆฐ ๋’ค์— ์ผ์–ด๋‚˜๋Š” ๊ฒƒ

promise ๊ฐ์ฒด ์‚ฌ์šฉ ์ด์œ 

  • ๋น„๋™๊ธฐ๋กœ ์ฒ˜๋ฆฌ๋˜๋Š” ์ž‘์—…์˜ ๊ฒฐ๊ณผ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด
  • ๊ฐ์ฒด์˜ ์ฝœ๋ฐฑํ•จ์ˆ˜๋กœ ์ž‘์—…์„ ์ง€์ • => โ€˜์ˆœ์„œ ๋ณด์žฅโ€™
  • ์ฝœ๋ฐฑํ•จ์ˆ˜๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์•„์ง€๋ฉด, ์ˆ˜์ •์ด ๋ณต์žก & ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ ๋–จ์–ด์ง
  • then()์„ ์ด์–ด๋ถ™์—ฌ ์ฝœ๋ฐฑํ•จ์ˆ˜ ์ž‘์—…์„ ์ฒ˜๋ฆฌ => ์ฝœ๋ฐฑ์ง€์˜ฅ ๋ฐฉ์ง€

promise ํŠน์ง•

  • Promise ์ž‘์—…์˜ ๊ฒฐ๊ณผ๋Š” resolve์™€ reject๋กœ ๋ฐ›์„ ์ˆ˜ ์žˆ์Œ
  • Promise ์ž‘์—…์ด ์„ฑ๊ณต์‹œ resolve๋กœ ๋ฐ›์Œ
  • ์‚ดํŒจ์‹œ reject๋กœ ๋ฐ›์Œ

fetch

JavaScript ๋‚ด์žฅ API๋กœ ๋น„๋™๊ธฐ ํ†ต์‹  ๋„คํŠธ์›Œํฌ๋ฅผ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๋Š” ๊ธฐ์ˆ 
fetch ํ•จ์ˆ˜๋Š” url๋กœ ์š”์ฒญ์„ ๋ณด๋‚ธ ๊ฒฐ๊ณผ๋กœ Promise ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜

fetch๋Š” promise ์ด๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋ฒ„ ์‘๋‹ต์„ ๋ฐ›์•„์•ผ ํ•œ๋‹ค

fetch ํŠน์ง•

  • Fetch๋กœ ๋ฐ˜ํ™˜๋˜๋Š” Promise ๊ฐ์ฒด๋Š” error๋ฅผ reject ํ•˜์ง€ ์•Š์Œ
  • ๋Œ€์‹  ok ์ƒํƒœ์ธ resolve ๋ฐ˜ํ™˜
  • ์ฆ‰, ๋ณ„๋„๋กœ ์—๋Ÿฌ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ค˜์•ผํ•จ
  • Fetch๋Š” ์ฟ ํ‚ค๋ฅผ ๋ณด๋‚ด๊ฑฐ๋‚˜ ๋ฐ›์ง€ ์•Š์Œ
  • ์ฟ ํ‚ค๋ฅผ ์ „์†กํ•˜๊ธฐ ์œ„ํ•ด์„  ์ž๊ฒฉ์ฆ๋ช…(credentials) ์˜ต์…˜ ์„ค์ •ํ•ด์ค˜์•ผ ํ•จ

async & await

fetch์˜ ๋‹จ์ ์„ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด ๋“ฑ์žฅ

  • fetch๋Š” ๋น„๋™๊ธฐ๋กœ ๋™์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฒฐ๊ณผ ์žฅ๋‹ด๋ถˆ๊ฐ€
  • ์ฝœ๋ฐฑํ•จ์ˆ˜๋กœ ์ฒ˜๋ฆฌํ•ด์•ผ๋งŒ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์žฅ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค๋Š” ๋‹จ์  ์กด์žฌ
  • ์ด ๋‹จ์ ์„ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด ๋“ฑ์žฅํ•œ ๊ฒƒ์ด async์™€ await

๊ทธ๋Ÿฌ๋‹ˆ๊นŒ!!!

  1. async๋ผ๊ณ  ๋น„๋™๊ธฐ ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•ด ๋น„๋™๊ธฐ ์ž‘์—…์— await๋ฅผ ๋ถ™์—ฌ์ฃผ๋ฉด
  2. ๊ทธ ๋’ค์— ์ด ๊ฒฐ๊ณผ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ž‘์—…์—์„œ ๋™๊ธฐ์ฒ˜๋Ÿผ ์ฒ˜๋ฆฌ๋จ
  3. ์ฆ‰, ๋น„๋™๊ธฐ ์ž‘์—…์ด ์ด๋ฃจ์–ด์ง„ ํ›„์— ๊ทธ ๊ฒฐ๊ณผ๋กœ ์ž‘์—…์ด ์ˆ˜ํ–‰๋จ

์—ฌํŠผ.. fetch๋ฅผ ์‚ฌ์šฉํ•œ weather ์ฝ”๋“œ

const API_KEY = '123';

function onGeoOk(position) {
  const lon = position.coords.longitude;
  const lat = position.coords.latitude;
  const url = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${API_KEY}&units=metric`;
  fetch(url)
    .then((response) => response.json())
    .then((data) => {
      const weather = document.querySelector('#weather span:first-child');
      const city = document.querySelector('#weather span:last-child');
      weather.innerText = `${data.weather[0].main} / ${data.main.temp}`;
      city.innerText = data.name;
    });
}

function onGeoError() {
  alert("Can't find you. No weather for you");
}

console.log('Requesting geolocation...');

navigator.geolocation.getCurrentPosition(onGeoOk, onGeoError);

interesting!!


Ref. Promise, Fetch, Async, Await ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž, ํ—ฌ๋กœ์ฝ”๋”ฉ

profile
ํ˜„์‹ค์ ์ธ ๋ชฝ์ƒ๊ฐ€

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