ํฐ์คํ ๋ฆฌ ๋ทฐ
[๐ฆ] ์๋~ ์ง๋๋ฒจ๋ก ์๋ ค๋๋ฆด๊ฒ์! (1) - promise
์ฑ._. 2025. 1. 16. 00:34
Promise
fetch๋ Promise๋ฅผ return ํ๋ ํจ์์ด๋ค.
๐fetch๋? ์ธ๋ถ์์ ๋ฐ์ดํฐ(์ ๋ณด)๋ฅผ ๊ฐ์ ธ์ค๋ ํจ์
๐Promise๊ฐ ์๋ ๊ฒฝ์ฐ์ ๋น๊ตํ๊ธฐ (document.querySelector("์ ํ์") vs fetch("์ฃผ์"))
document.querySelector()
const ์์ = document.querySelector(".ํด๋์ค์ด๋ฆ");
console.log(์์) // html ์์๊ฐ ๋์จ๋ค.
// ex) <div class="ํด๋์ค์ด๋ฆ"></div>
๋ณ์์ ๋ด์ผ๋ฉด ์ํ๋ ๊ฒ์ด ๋์จ๋ค.
fetch()
const response = fetch("https://jsonplaceholder.typicode.com/todos/1");
console.log(response);

์ด์ํ ๋ฐ์ดํฐ๊ฐ ๋์จ๋ค. ์ด๊ฒ ๋ฐ๋ก promise ์ด๋ค.
์ด๋ฐ promise๋ฅผ ๋ง๋๋ฉด ๋ค๋ฅธ ๋ฐฉ๋ฒ์ ์ฐพ์์ผ ํ๋ค.
๐๐ป๊ธ๋ก ์ ๋ฆฌํ ๋ด์ฉ
1. fetch ํจ์๋ฅผ ์ผ๋ฐ ํจ์์ฒ๋ผ ์ฌ์ฉํ ์, ์ํ๋ ๋ฐ์ดํฐ์ ํํ๋ก ๋ณํํ ์ ์๋ค.
2. JS์์ ์ฌ์ฉํ๊ธฐ ์ํด์๋ json์ ์ด์ฉํ ๋ณํ์ด ํ์ํ๋ค. ๊ทธ ๋จ๊ณ๋ ๋ค์๊ณผ ๊ฐ๋ค.
3. fetch ํจ์๋ promise๋ผ๋ ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ค.
4. promise ๊ฐ์ฒด๋ then์ด๋ผ๋ ๋ฉ์๋๋ฅผ ๊ฐ๊ณ ์๋ค.
5. then ๋ฉ์๋๋ ์ธํ์ผ๋ก ์ฝ๋ฐฑํจ์๋ฅผ ๋ฐ๋๋ค.
6. ๊ทธ ์ฝ๋ฐฑํจ์์ ์ธ์์๋ promise ๊ฐ์ฒด์ return ๊ฐ์ ๋ฃ์ด์ค๋ค.
์ฝ๋ฐฑํจ์ ์์์๋ json ๋ฉ์๋๋ฅผ ํตํด JS์์ ํ์ฉ ๊ฐ๋ฅํ ๊ฐ์ฒด ํํ๋ก ๋ฐ๊ฟ์ค๋ค.
7. json๋ promise๋ฅผ return ํ๊ธฐ ๋๋ฌธ์ ๋ then์ผ๋ก ๋ฐ์ ์ ์๋ค.
8. ๊ฒฐ๊ตญ JS์์ ํ์ฉ ๊ฐ๋ฅํ array ํํ๋ก ๊ฒฐ๊ณผ๊ฐ์ ์ป์ ์ ์๋ค.
๐๐ป์ด๋ฐ ๋ณต์กํ ๋ฐฉ๋ฒ์ ์ ์ฌ์ฉํ ๊น?
- ๋ง์ฝ ์ธํ๊ธฐ๋ฅผ ๋๋ฆฌ๊ณ , ์ธํ๊ธฐ๊ฐ ์๋ฃ๋ ๋๊น์ง ๋ค๋ฅธ ์์ ์ ๋ชปํ๋ค๋ฉด ๊ต์ฅํ ๋นํจ์จ์ ์ผ ๊ฒ์ด๋ค.
- ์ผ๋ฐ์ ์ผ๋ก ์ค๋ ๊ฑธ๋ฆฌ๋ ์ธํ๊ธฐ๋ฅผ ์๋ํ ํ ๋ค๋ฅธ ์ผ์ ํ๋ค. ๊ทธ๋ฆฌ๊ณ ์ธํ๊ธฐ๋ ์์์ ๋์๊ฐ๋ค ๋๋ธ๋ค.
- fetch๋ ์ธ๋ถ์์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๋ ์ผ์ด๋ค. ์ฆ, ์ค๋ ๊ฑธ๋ฆฐ๋ค. ๋ง์น ์ธํ๊ธฐ์ฒ๋ผ
- ์ฝ๋๋ ์๋ ์์์ ์๋๋ก ์์๋๋ก ์คํ๋๋ค.
๊ทธ๋ฐ๋ฐ ์์ ์๋ ์ฝ๋๊ฐ ์๋ฃ๋๋๋ฐ ๋๋ฌด ๋๋ฆฌ๋ฉด ์๋ ์ฝ๋๋ค์ ์คํ๋์ง ๋ชปํ๋ค. - ์ค๋ ๊ฑธ๋ฆด ์ ์๋ ์ฝ๋๋ ๋น๋๊ธฐ๋ก ์คํํ์ฌ ๋ค์ ์ฝ๋๋ ๊ณ์ ์งํ๋๋๋ก ๋ง๋๋ ๊ฒ์ด ๋ ์ข๋ค.
- ๋์ .then์ ํตํด ์ค๋ ๊ฑธ๋ฆฌ๋ ์์ ์ด ๋๋๋ฉด .then() ํจ์๋ก ๊ฒฐ๊ณผ๊ฐ ์ ๋ฌ๋๊ฒ ๋ง๋ ๊ฒ์ด๋ค.
console.log("์์")
fetch("https://jsonplaceholder.typicode.com/todos/1")
.then((response) => {
return response.json();
})
.then((data) => {
console.log(data); // 2. ์๋ ๋์ค์ ์ถ๋ ฅ๋จ
});
console.log("fetch ๋๋ฌ๋?"); // 1. ์๊ฐ ๋จผ์ ์ถ๋ ฅ๋๊ณ

๐fetch ์์ฑ๋ฒ
1. fetch์ ์ธ์๋ก ์ฃผ์ ์์ฑ
fetch(์ฃผ์)
2. ๋ค์ .then(ํจ์) ์์ฑ
fetch(์ฃผ์).then(() => { })
3. then์ ์ฝ๋ฐฑํจ์์ ํ๋ผ๋ฏธํฐ ๋ฐ return ํ๋ผ๋ฏธํฐ.json() ์์ฑ
fetch(์ฃผ์).then((ํ๋ผ๋ฏธํฐ์ด๋ฆ) => {
return ํ๋ผ๋ฏธํฐ์ด๋ฆ.json();
})
4. .then์ ํ ๋ฒ ๋ ์์ฑ ๋ฐ ํ๋ผ๋ฏธํฐ ์ฌ์ฉ
fetch(์ฃผ์).then((ํ๋ผ๋ฏธํฐ์ด๋ฆ) => {
return ํ๋ผ๋ฏธํฐ์ด๋ฆ.json();
}).then((ํ์ํ๋ฐ์ดํฐ) => {
// ๋ฐ์ดํฐ ์ฌ์ฉ
console.log(ํ์ํ๋ฐ์ดํฐ)
})
options
๐options๋?
const options = {
method: "GET",
headers: {
accept: "application/json",
Authorization:
"Bearer eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJmOGE3YjI2ZmRmZmFiYmVjMjNhZWE0NjZkMzFkODM2NSIsIm5iZiI6MTcyOTU3OTAwMi43Nzc1MzgsInN1YiI6IjY0NzFhM2NmYTE5OWE2MDBmOTQxZjlhYyIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.H2-DM_YlyvlwTr4KI-xacY2oeCrSVxybY2tRcPFRhwU",
},
};
fetch(
"https://api.themoviedb.org/3/movie/upcoming?language=en-US&page=1",
options
)
.then((res) => res.json())
.then((res) => console.log(res))
.catch((err) => console.error(err));
๋ฐฑ์๋ ๊ฐ๋ฐ์๊ฐ ์ถ๊ฐ์ ์ธ ์ ๋ณด๋ฅผ ์์ฒญํ ๊ฒฝ์ฐ์ ๋ฃ๋๋ค.
- method
- GET, POST, PUT, PATCH, DELETE - headers
- accept: "application/json": ์๋ฒ์๊ฒ JSON ํ์์ ์๋ต์ ์์ฒญํ๋ค.
- Authorization: ์ธ์ฆ ์ ๋ณด ๋ฃ๋ ๊ณณ (์์ ๋๋ ์์)
์์ฑ๋ฒ: fetch์ ๋ ๋ฒ์งธ ์ธ์๋ก ๋ฃ๋๋ค.
fetch(์ฃผ์, { method: "GET", headers: { ... } })
method
- method์ ์ข
๋ฅ
- GET: ์กฐํ
- POST: ์ถ๊ฐ, ๋ฑ๋ก
- PUT: ์ ์ฒด ์์
- PATCH: ์ผ๋ถ ์์
- DELETE: ์ญ์
[๋ฌธ์]

method์ ์ฃผ์๊ฐ ์๋ค.
์ฃผ๋ก ๋ฐฑ์๋ ๊ฐ๋ฐ์๋ method, ์ฃผ์๋ฅผ ์ ๊ณตํด์ฃผ๋ฉฐ ์ฐ๋ฆฌ๋ ์ฌ์ฉ๋ง ํ๋ฉด ๋๋ค.
- method: GET
- ์ฃผ์: /posts
- ์๋ฏธ: ๊ฒ์๋ฌผ ์ฌ๋ฌ ๊ฐ(posts)๋ฅผ ์กฐํ(GET)ํ๋ค.
- ํ์ง๋ง GET ๋ฉ์๋๋ ์๋ตํ๋ฉด ๊ทธ๋ฅ fetch๊ฐ ์์์ GET์ด๋ผ๊ณ ์๊ฐํ๋ค.
fetch("https://jsonplaceholder.typicode.com/posts", { method: "GET" })
// method ์๋ต ๊ฐ๋ฅ
fetch("https://jsonplaceholder.typicode.com/posts")
- method: GET
- ์ฃผ์: /posts/1
- ์๋ฏธ: id๊ฐ 1์ธ ๊ฒ์๋ฌผ(/posts/1)์ ์กฐํ(GET)ํ๋ค.
fetch("https://jsonplaceholder.typicode.com/posts/1", { method: "GET" })
- method: GET
- ์ฃผ์: /comments?postId=1 vs /post/1/comments
- ๋ชจ๋ ๋๊ธ ์ค id๊ฐ 1์ธ ๊ฒ์๋ฌผ(/comments?postId=1)์ ํํฐ๋งํด์ ์กฐํ(GET)์จ๋ค.
fetch("https://jsonplaceholder.typicode.com/comments?postId=1", { method: "GET" })
- method: POST
- ์ฃผ์: /posts
- ๊ฒ์๋ฌผ๋ค(/posts)์ ๋ฐ์ดํฐ๋ฅผ **์ถ๊ฐ(POST)**ํ๋ค
- POST ๋ฉ์๋๋ body๋ฅผ ๋ฃ์ ์ ์์
- body์ ์ถ๊ฐํ ๋ฐ์ดํฐ๋ฅผ ๋ฃ๋๋ค.
fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
body: JSON.stringify({
title: 'foo',
body: 'bar',
userId: 1,
}),
headers: {
'Content-type': 'application/json; charset=UTF-8',
},
})
.then((response) => response.json())
.then((json) => console.log(json));
- method: PUT
- ์ฃผ์: /posts/1
- id๊ฐ 1์ธ ๊ฒ์๋ฌผ(/posts/1)์ **์์ (PUT)**ํ๋ค.
- PUT ๋ฉ์๋๋ body๋ฅผ ๋ฃ์ ์ ์์
- body์ ์์ ํ ๋ฐ์ดํฐ๋ฅผ ๋ฃ๋๋ค.
fetch('https://jsonplaceholder.typicode.com/posts/1', {
method: 'PUT',
body: JSON.stringify({
id: 1,
title: 'foo',
body: 'bar',
userId: 1,
}),
headers: {
'Content-type': 'application/json; charset=UTF-8',
},
})
.then((response) => response.json())
.then((json) => console.log(json));
- method: PATCH
- ์ฃผ์: /posts/1
- id๊ฐ 1์ธ ๊ฒ์๋ฌผ(/posts/1)์ **์ผ๋ถ ์์ (PATCH)**ํ๋ค.
- PATCH ๋ฉ์๋๋ body๋ฅผ ๋ฃ์ ์ ์์
- title๋ง ์์
fetch('https://jsonplaceholder.typicode.com/posts/1', {
method: 'PATCH',
body: JSON.stringify({
title: 'foo',
}),
headers: {
'Content-type': 'application/json; charset=UTF-8',
},
})
.then((response) => response.json())
.then((json) => console.log(json));
- method: DELETE
- ์ฃผ์: /posts/1
- id๊ฐ 1์ธ ๊ฒ์๋ฌผ(/posts/1)์ ์ญ์ (DELETE)ํ๋ค.
fetch('https://jsonplaceholder.typicode.com/posts/1', {
method: 'DELETE',
});
catch
๐catch๋?
์๋ฌ๊ฐ ๋ฐ์ํ ๋ ์ด๋ป๊ฒ ์ฒ๋ฆฌํ ์ง ์กฐ์ํ๋ ์ฉ๋์ด๋ค.
const options = {
method: "GET",
headers: {
accept: "application/json",
Authorization:
"Bearer eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJmOGE3YjI2ZmRmZmFiYmVjMjNhZWE0NjZkMzFkODM2NSIsIm5iZiI6MTcyOTU3OTAwMi43Nzc1MzgsInN1YiI6IjY0NzFhM2NmYTE5OWE2MDBmOTQxZjlhYyIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.H2-DM_YlyvlwTr4KI-xacY2oeCrSVxybY2tRcPFRhwU",
},
};
fetch(
"https://api.themoviedb.org/3/movie/upcoming?language=en-US&page=1",
options
)
.then((res) => res.json())
.then((res) => console.log(res))
.catch((err) => console.error(err));
์ฐ์ต์ฉ ์ธํ
[index.html]
<!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>
<div id="movie-container"></div>
<script src="script.js"></script>
</body>
</html>
[script.js]
const options = {
method: "GET",
headers: {
accept: "application/json",
Authorization:
"Bearer eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJmOGE3YjI2ZmRmZmFiYmVjMjNhZWE0NjZkMzFkODM2NSIsIm5iZiI6MTcyOTU3OTAwMi43Nzc1MzgsInN1YiI6IjY0NzFhM2NmYTE5OWE2MDBmOTQxZjlhYyIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.H2-DM_YlyvlwTr4KI-xacY2oeCrSVxybY2tRcPFRhwU",
},
};
fetch(
"https://api.themoviedb.org/3/movie/upcoming?language=en-US&page=1",
options
)
.then((res) => res.json())
.then((res) => {
// ๋ง์ฝ ์ฌ๊ธฐ ์๋ ์ฝ๋์์ ์๋ฌ๊ฐ ๋ฐ์ํ๋ฉด catch๋ก ์ด๋ํฉ๋๋ค.
const movies = res.results;
const movieContainer = document.getElementById("movie-container");
movies.forEach((movie) => {
movieContainer.innerHTML += `<div>${movie.title}</div>`;
});
})
์ผ๋ถ๋ฌ ์๋ฌ๋ฅผ ๋ฐ์์์ผ๋ณธ๋ค.
const options = {
method: "GET",
headers: {
accept: "application/json",
Authorization:
"Bearer eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJmOGE3YjI2ZmRmZmFiYmVjMjNhZWE0NjZkMzFkODM2NSIsIm5iZiI6MTcyOTU3OTAwMi43Nzc1MzgsInN1YiI6IjY0NzFhM2NmYTE5OWE2MDBmOTQxZjlhYyIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.H2-DM_YlyvlwTr4KI-xacY2oeCrSVxybY2tRcPFRhwU",
},
};
fetch(
"https://api.themoviedb.org/3/movie/upcoming?language=en-US&page=1",
options
)
.then((res) => res.json())
.then((res) => {
const movies = res.results;
// ์ฝ๋ ๋ง๊ฐ๋จ๋ฆผ(document.get ์ด๋ผ๋ ํจ์๋ ์์ด์!)
const movieContainer = document.get("movie-container");
movies.forEach((movie) => {
movieContainer.innerHTML += `<div>${movie.title}</div>`;
});
})
catch๋ฅผ ๋ถ์ฌ ์๋ฌ๋ฅผ ์ฒ๋ฆฌํ๋ค.
const options = {
method: "GET",
headers: {
accept: "application/json",
Authorization:
"Bearer eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJmOGE3YjI2ZmRmZmFiYmVjMjNhZWE0NjZkMzFkODM2NSIsIm5iZiI6MTcyOTU3OTAwMi43Nzc1MzgsInN1YiI6IjY0NzFhM2NmYTE5OWE2MDBmOTQxZjlhYyIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.H2-DM_YlyvlwTr4KI-xacY2oeCrSVxybY2tRcPFRhwU",
},
};
fetch(
"https://api.themoviedb.org/3/movie/upcoming?language=en-US&page=1",
options
)
.then((res) => res.json())
.then((res) => {
const movies = res.results;
const movieContainer = document.get("movie-container");
movies.forEach((movie) => {
movieContainer.innerHTML += `<div>${movie.title}</div>`;
});
})
.catch((err) => {
const errorDiv = document.createElement("div");
errorDiv.textContent = "์๋ฌ๊ฐ ๋ฐ์ํ์ต๋๋ค. ๋์ค์ ๋ค์ ์๋ํด์ฃผ์ธ์.";
document.body.appendChild(errorDiv);
});'Language > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [๐ฆ] ์ด๋ฒคํธ ๋ฒ๋ธ๋ง ๐ซง (0) | 2025.01.17 |
|---|---|
| ๊ฐํน๊ฐ๋ค ํท๊ฐ๋ฆด ์ ์๋ ๋ฌธ์ ๋ค ! - ๋ฌธ์ ์ํ ์ค๋ต ํ์ด (1) | 2025.01.16 |
| [๐ฆ] ๋ชจ๋ฌ์ฐฝ ์ฌ๋ฌ ์ถ๊ฐ ๊ธฐ๋ฅ ๊ตฌํํ๊ธฐ (0) | 2025.01.15 |
| ๊ทธ๋, id๋ ๊ฐ์ ธ์์ด. ์ด์ ์ด๋กํ ๊ฑด๋ฐ?! - find() (0) | 2025.01.15 |
| [๐ฆ] text ์ผํ์ ๋น๊ต - textContent / innerText / innerHTML (0) | 2025.01.12 |