# `fetch` API 1`fetch` 함수는 네트워크 리소스를 요청하고 응답을 처리하는 API예요. 비동기적으로 동작하고, `Promise<Response>` 객체를 반환해요. `fetch` 함수를 활용하면 클라이언트와 서버 간 데이터를 쉽게 주고받을 수 있어, REST API와 같은 서비스와의 통신을 효율적으로 처리할 수 있어요.`XMLHttpRequest`보다 간결한 문법을 제공하고, `async/await`와 함께 사용하면 가독성이 뛰어나다는 장점도 있어요. ## 시그니처 2```typescriptfetch(input: RequestInfo, init?: RequestInit): Promise<Response>``` 3## 매개변수 - `input` (필수): 요청할 URL 또는 `Request` 객체예요.- `init` (선택): 요청의 옵션을 담은 객체예요. - `method`: HTTP 요청 방식 (GET, POST, PUT, DELETE 등) - `headers`: 요청에 포함할 헤더 정보 (예: { 'Content-Type': 'application/json' }) - `body`: 요청 본문 (예: JSON.stringify({ name: 'John' })) - `mode`: 요청 모드 (cors, no-cors, same-origin) - `credentials`: 쿠키 포함 여부 (omit, same-origin, include) - `cache`: 캐시 정책 (default, no-store, reload, force-cache 등) - `redirect`: 리디렉션 처리 방식 (follow, error, manual) ## 반환값 `fetch`는 `Promise<Response>` 객체를 반환해요. - `ok`: 응답이 성공(200~299)했는지 여부 (true / false) - `status`: HTTP 상태 코드 (200, 404, 500 등) - `headers`: 응답 헤더 (Headers 객체) - `json`(): 응답을 JSON으로 변환 (Promise<object>) - `text`(): 응답을 문자열로 변환 (Promise<string>) - `blob`(): 응답을 Blob 객체로 변환 (Promise<Blob>) ## 사용 예제 4### 기본 GET 요청 ```javascriptfetch('https://jsonplaceholder.typicode.com/posts/1') .then(response => { if (!response.ok) { throw new Error('네트워크 응답이 올바르지 않아요.'); } return response.json(); }) .then(data => console.log(data)) .catch(error => console.error('오류 발생:', error));``` ### POST 요청 예제 ```javascriptfetch('https://api.example.com/user', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ name: 'John', age: 30 }),}) .then(response => { if (!response.ok) { throw new Error('요청이 실패했어요.'); } return response.json(); }) .then(data => console.log('서버 응답:', data)) .catch(error => console.error('오류 발생:', error));``` 5### async/await 사용 ```javascriptasync function fetchData() { try { const response = await fetch('https://jsonplaceholder.typicode.com/posts/1'); if (!response.ok) { throw new Error('네트워크 응답이 올바르지 않아요.'); } const data = await response.json(); console.log(data); } catch (error) { console.error('오류 발생:', error); }} fetchData();```