본문 바로가기
Dev/React

[React] fetch + async + await 실습

by 컴포넌트설계자 2026. 4. 9.

비동기 통신 Axios fetch + async +  await 
1) Fetch 함수 – IE에서 지원이잘 안됨. : 원격 API를 간편하게 호출 할 수 있도록 브라우져에서 제공하는 fetch()함수 - window.fetch(url, option);
2) 외부 lib 사용(Promise기반의 Http 비동기 통신라이브러리) : 원격 API를 호출 하는 라이브러리 - jQuerylib - Axios lib

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<h1>비동기 통신 fetch +async + await</h1>
<script>
console.log("시작")
     const test = async ()=>{
  try{      
       const res = await  fetch("https://jsonplaceholder.typicode.com/users")
     
     const jsonResult = await res.json();
     jsonResult.forEach((user, index)=>{
        console.log(index+"=" +user.id + "|" + user.name + "|" + user.email);
     
     });
    } catch(err){
        console.log(err);
    }
     }
test();
</script>
</body>
</html>

https://jsonplaceholder.typicode.com/users 백엔드가 없어서 일단 통신은 해당 url로 해준다.