๋ฐ์ํ
![[JAYโจ๏ธJS] ์๋ฒ์ ๋คํธ์ํฌ ์์ฒญ์ ๋ณด๋ด๊ณ ์๋ก์ด ์ ๋ณด๋ฅผ ๋ฐ์์ค๋ fetch() ๋ฉ์๋](http://t1.daumcdn.net/tistory_admin/static/images/xBoxReplace_250.png)
1๏ธโฃ fetch API ๋?
- API(์ ํ๋ฆฌ์ผ์ด์ ํ๋ก๊ทธ๋๋ฐ ์ธํฐํ์ด์ค)๊ฐ ๋ฑ์ฅํ๊ณ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์๋ฒ์ ์ฐ๊ฒฐํด์ผ ํ ํ์์ฑ์ด ์ปค์ง์ ๋ฐ๋ผ ํ์ฌ HTTP ์์ฒญ์ ์ค์์ฑ์ ์๋ฌด๋ฆฌ ๊ฐ์กฐํด๋ ์ง๋์น์ง ์๊ฒ ๋์๋ค.
- ์๋ฐ์คํฌ๋ฆฝํธ์๋ ์ด๋ฌํ ์์ฒญ์ ์ํํ ์ ์๋ ๋งค์ฐ ํธ๋ฆฌํ ๋ด์ฅ ๋ฉ์๋์ธ fetch() ๊ฐ ์๋ค.
- fetch()๋ HTTP ์์ฒญ์ ์ฌ์ฉ๋๋ Promise ๊ธฐ๋ฐ API ์ด๋ฉฐ, ์ด์ ์ XMLHttpRequest์ ๊ฐ์ ๋ณต์กํ ๋คํธ์ํฌ ์์ฒญ ์ ์ฐจ๋ฅผ ์ฌ์ฉํ๊ธฐ ์ฝ๋๋ก ๊ฐ์ ํ ๋ฌธ๋ฒ์ด๋ค.
2๏ธโฃ fetch API์ ์ฃผ์ ์ฅ์
- Fetch๋ promise ๊ธฐ๋ฐ์ผ๋ก, ๋น๋๊ธฐ ์์ ์ ์ฌ์ฉํ๊ณ ๊ด๋ฆฌํ๋ ๊ฒ์ ์ฝ๊ฒ ํด์ค๋ค.
- ๊ฐ๋จํ๊ณ ๊น๋ํ ๊ตฌ๋ฌธ์ ์ ๊ณตํ๋ฏ๋ก ์ฝ๋์ ๊ฐ๋ ์ฑ๊ณผ ์ ์ง ๊ด๋ฆฌ๊ฐ ๋์ฑ ์ฌ์์ง๋ค.
- catch ๋ฉ์๋๋ฅผ ํตํด ํจ์จ์ ์ด๊ณ ๊ฐ์ํ๋ ์ค๋ฅ ์ฒ๋ฆฌ ๋ฉ์ปค๋์ฆ์ ์ ๊ณตํ๋ค.
- IE๋ฅผ ์ ์ธํ ๋ชจ๋ ์ต์ ๋ธ๋ผ์ฐ์ ์ ํธํ๋๋ค. IE์ ๊ฒฝ์ฐ ํธํ์ ์ํด ํด๋ฆฌํ ์ฌ์ฉ.
- ๋ฉ์๋, ํค๋, ๋ณธ๋ฌธ ์ฝํ ์ธ ๋ฑ ์์ฒญ ๋ฐ ์๋ต์ ์ธ๋ถ ์ฌํญ์ ๋์ ์์ค์ผ๋ก ์ ์ดํ ์ ์๋ค.
3๏ธโฃ Promise 'then'์ ์ฌ์ฉํ fetch()
fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
- fetch๋ฉ์๋๋ฅผ ํ์ฉํด์ API๋ฅผ ํธ์ถํ๋ค. (ex. https://api.example.com/data')
- API๋ฅผ ์์ฒญํ ํ ์ดํ(resolved)์ด ๋๋ฉด response promise๋ฅผ ๋ฐํ.
- response promise๋ฅผ json ํ์์ผ๋ก ๋ณํ.
- promise๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์ดํ(resolved)๋๋ฉด ์ฝ์์ ๋ก๊ทธ๋ฅผ ์ฐ์ด์ ํ์ธ ๊ฐ๋ฅ.
- ์ด๋ค ์์ ์์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ฉด ํด๋น ์ค๋ฅ์ ๋ํ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅ.
API ํธ์ถ์ ๋ํ ์๋ต์ ์ป๊ธฐ ์ํ ๋ฉ์๋
response.text() ์๋ต์ ํ ์คํธ ํํ๋ก ๋ฐํ
response.json() ์๋ต์ ํ์ฑํด JSON ๊ฐ์ฒด๋ก ๋ณ๊ฒฝ
response.formData() ์๋ต์ FormData ๊ฐ์ฒด ํํ๋ก ๋ฐํ
response.blob() ์๋ต์ Blob(ํ์ ์ด ์๋ ๋ฐ์ด๋๋ฆฌ ๋ฐ์ดํฐ) ํํ๋ก ๋ฐํ
response.arrayBuffer() ์๋ต์ ArrayBuffer(๋ฐ์ด๋๋ฆฌ ๋ฐ์ดํฐ๋ฅผ ๋ก์ฐ ๋ ๋ฒจ๋ก ํํํ ๊ฒ) ํํ๋ก ๋ฐํ
4๏ธโฃ async/await๋ฅผ ์ด์ฉํ fetch()
async function fetchData() {
try {
let response = await fetch('https://api.example.com/data');
let data = await response.json();
console.log(data);
} catch (error) {
console.error('Error:', error);
}
}
fetchData();
- promise .then ๋ฉ์๋๋ณด๋ค ๋์ฑ ๊น๋ํ๊ฒ fetch API๋ฅผ ํธ์ถํ ์ ์๋ ์คํ ๋ฌธ๋ฒ์ผ๋ก async/await์ ํ์ฉํ ์ ์๋ค.
- ํจ์ ์์ async๋ฅผ ๋ถ์ด๋ฉด ํด๋น ํจ์๋ ํญ์ promise๋ฅผ ๋ฐํํ๊ณ ,
promise๊ฐ ์๋ ๊ฒ์ promise๋ก ๊ฐ์ ๊ฐ์ธ์ ์ดํ(resolved) ์ํ์ promise๊ฐ ๋ฐํ๋๋๋ก ํ๋ค. - await๋ async ํจ์ ์์์๋ง ๋์ํ๋ฉฐ, promise๊ฐ ์ฒ๋ฆฌ๋ ๋๊น์ง ๊ธฐ๋ค๋ฆฌ๋ฉฐ ๋๊ธฐ์ ์ผ๋ก ์คํ๋๋ค.
- ์ผ๋ฐ์ ์ผ๋ก fetch ์์ฒญ์์ ์๋ต์ awaitํ๊ณ , ์๋ต์ JSON ํ์์ผ๋ก ๋ณํํ๋ await ํ๋ ๋ ๊ฐ์ await ํธ์ถ๋ก ๊ตฌ์ฑ.
- ์ด๋ค ์์ ์์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ฉด ํด๋น ์ค๋ฅ์ ๋ํ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํ๊ธฐ ์ํด try/catch ๋ฌธ์ ํ์ฉํด์ ์์ธ์ฒ๋ฆฌ
5๏ธโฃ fetch() ์ฌ์ฉ ์ ์์ธ ์ฒ๋ฆฌ
fetch('https://api.example.com/data')
.then(response => {
// response.ok โ ์๋ต ์ํ๊ฐ 200๊ณผ 299 ์ฌ์ด์ ์๋ ๊ฒฝ์ฐ true
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json();
})
.then(data => console.log(data))
- Fetch๋ก ์์ ํ ๋ ์ฃผ์ํด์ผ ํ ์ค์ํ ์ ์ ์์ธ๋ฅผ ์ฒ๋ฆฌํ๋ ๋ฐฉ์!
- XMLHttpRequest์ ๊ฐ์ ์ด์ API์ ๋ฌ๋ฆฌ fetch()๋ ๋คํธ์ํฌ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ ๋์๋ง Promise ๊ฑฐ๋ถ(rejects) ๊ฐ์ ๋ฐํ.
- HTTP 404 ๋๋ 500 ์ค๋ฅ๊ฐ ๋ฐ์ํด๋ fetch๋ rejects ์ฒ๋ฆฌ๋์ง ์๊ธฐ ๋๋ฌธ์ ์ค๋ฅ์ ๋ํด ๋ช ์์ ์ธ ์์ธ ์ฒ๋ฆฌ๋ฅผ ํด์ผ ํ๋ค.
response ๊ฐ์ฒด์ property
response.status ์๋ต์ HTTP ์ฝ๋
response.ok ์๋ต ์ํ๊ฐ 200๊ณผ 299 ์ฌ์ด์ ์๋ ๊ฒฝ์ฐ
trueresponse.headers ๋งต๊ณผ ์ ์ฌํ ํํ์ HTTP ํค๋
๐๐ปโโ๏ธ ์์ฝ!
- fetch()๋ HTTP ์์ฒญ์ ์ฌ์ฉ๋๋ Promise ๊ธฐ๋ฐ API ์ด๋ฉฐ, ๋ณต์กํ ๋คํธ์ํฌ ์์ฒญ ์ ์ฐจ๋ฅผ ์ฌ์ฉํ๊ธฐ ์ฝ๋๋ก ๊ฐ์ ํ ๋ฌธ๋ฒ.
- promise๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ .then() ๋ฉ์๋์ async/await ํตํด fetch API ์์ฒญ ๊ฐ๋ฅ.
- async/await์ ํตํด fetch() ๋ฉ์๋๋ฅผ ์คํํ๋ ๊ฒฝ์ฐ ์ผ๋ฐ์ ์ธ fetch ์์ฒญ์ ๋ ๊ฐ์ await ํธ์ถ๋ก ๊ตฌ์ฑ.
- Fetch๋ก ์์ ํ ๋ ์์ธ์ฒ๋ฆฌ๋ฅผ ๊ผญ ํด์ค ๊ฒ!
๋ฐ์ํ