Promise- ํ์ ์ฒ๋ฆฌ์ ์ฒด์ด๋(Chaining)
๋น๋๊ธฐ ์ฒ๋ฆฌ ์ํ ๊ฒฐ๊ณผ์ ๋ฐ๋ฅธ ํ์ ์ฒ๋ฆฌ
resolve / reject ์ฝ๋ฐฑ์ ์ํด ํ๋ก๋ฏธ์ค์ ๋น๋๊ธฐ ์ฒ๋ฆฌ ์ํ๊ฐ ๋ณํํ๋ฉด (pending -> fullfilled)
ํ์ ์ฒ๋ฆฌ๋ฅผ ์ํํ ์ฝ๋๋ฅผ ์์ฑํด์ผ ํ๋๋ฐ Promise ๊ฐ์ฒด๋ ๊ทธ๋ฌํ ์ฝ๋ ์์ฑ์ ํ ์ ์๋ ๋ณ๋์ ๋ฉ์๋๋ฅผ ์ ๊ณตํ๋ค.
then() , catch(), finally()
*ํ์ ์ฒ๋ฆฌ: ์๋ต ๊ฒฐ๊ณผ ๊ฐ ์ถ์ถ or ์ฌ์ฉ
1. then(callback) - ๋น๋๊ธฐ ์ฒ๋ฆฌ ์ฑ๊ณต ์ ํธ์ถ
๋น๋๊ธฐ ์ฒ๋ฆฌ๊ฐ ์ฑ๊ณตํ์ฌ resolve ์ฝ๋ฐฑ์ด ํธ์ถ๋์์ ๊ฒฝ์ฐ ๋์ํ๋ฉฐ, ์ค์ ๋์ํ๋ ๊ฒ์ then() ์ด ์๋ then(callback) ํจ์์ ์ธ์๋ก ์ ๋ฌ๋ ์ฝ๋ฐฑ ํจ์์ด๋ค.
1
2
3
|
const myPromise = new Promise(executor);
myPromise.then((response) => console.log(\`์๋ต ๊ฒฐ๊ณผ: ${response}\`));
// ์๋ต ๊ฒฐ๊ณผ: some data
|
cs |
2. catch(callback) - ๋น๋๊ธฐ ์ฒ๋ฆฌ ์คํจ ์ ํธ์ถ
๋น๋๊ธฐ ์ฒ๋ฆฌ๊ฐ ์คํจํ์ฌ reject ์ฝ๋ฐฑ์ด ํธ์ถ๋์์ ๊ฒฝ์ฐ ๋์ํ๋ ํจ์
1
2
3
4
5
6
|
const myPromise = new Promise(executor);
myPromise
.then((response) => console.log(\`์๋ต ๊ฒฐ๊ณผ: ${response}\`))
.catch((error) => console.error(\`error: ${error}\`));
// error: ์๋ต ์คํจ
|
cs |
3. finally(callback)
๋น๋๊ธฐ ์ฒ๋ฆฌ ์ฑ๊ณต/์คํจ ์ฌ๋ถ์ ์๊ด์์ด ๋ง์ง๋ง์ ํ ๋ฒ์ ๋ฌด์กฐ๊ฑด ๋์ํ๋๋ก ๋ณด์ฅ๋๋ ํจ์
1
2
3
4
5
6
7
8
|
const myPromise = new Promise(executor);
myPromise
.then((response) => console.log(\`์๋ต ๊ฒฐ๊ณผ: ${response}\`))
.catch((error) => console.error(\`error: ${error}\`));
.finally(() => console.log('์์ฒญ ์ํ ์๋ฃ'));
// ์๋ต ๊ฒฐ๊ณผ: some data or error: ์๋ต ์คํจ
// ์์ฒญ ์ํ ์๋ฃ(๋ฌด์กฐ๊ฑด ํ ๋ฒ์ ๋์)
|
cs |
ํ๋ก๋ฏธ์ค ์ฒด์ด๋
์๋์ ๊ฐ์ด ๊ฐ๊ฐ์ ํ์ ์ฒ๋ฆฌ ๋ฉ์๋๋ค์ด ์ด์ด์ ํธ์ถํ ์ ์๋๋ก ๋์ํ๋ ์ด์ ๋ ํด๋น ๋ฉ์๋๋ค์ด Promise ๊ฐ์ฒด๋ฅผ ๋ฐํํ๊ธฐ ๋๋ฌธ์ด๋ค.
1
2
3
4
5
6
7
8
|
onst myPromise = new Promise(executor);
myPromise
.then((response) => console.log(\`์๋ต ๊ฒฐ๊ณผ: ${response}\`))
.catch((error) => console.error(\`error: ${error}\`));
.finally(() => console.log('์์ฒญ ์ํ ์๋ฃ'));
// ์๋ต ๊ฒฐ๊ณผ: some data or error: ์๋ต ์คํจ
// ์์ฒญ ์ํ ์๋ฃ(๋ฌด์กฐ๊ฑด ํ ๋ฒ์ ๋์)
|
cs |
ํ๋ก๋ฏธ์ค ์ฒด์ด๋์ ํ์ฉํ์ฌ ์ฝ๋ฐฑ ํฌ ๊ฐ์
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
onst url = '[https://jsonplaceholder.typicode.com'](https://jsonplaceholder.typicode.com');
const promiseGet = url => {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.send();
xhr.onload = () => {
if (xhr.status === 200) {
// ์๋ต ์ฑ๊ณต
resolve(JSON.parse(xhr.response));
} else {
// ์๋ฌ ์ฒ๋ฆฌ
reject(new Error(xhr.status));
}
};
});
};
promiseGet(\`${url}/posts/1\`) // post id๊ฐ 1๋ฒ์ธ post ์กฐํ
.then((user) => promiseGet(\`${url}/users/${user.userId}\`)) // ํด๋น post๋ฅผ ์์ฑํ user์ id๋ฅผ ์ด์ฉํ์ฌ user ์ ๋ณด ์กฐํ
.then(userInfo => console.log(userInfo))
.catch(error => console.error(error));
|
cs |
-> then() ์ ํตํด ๋ณ๋์ ๋ค์ฌ์ฐ๊ธฐ ์์ด ์ฐ์์ ์ผ๋ก ๋น๋๊ธฐ ์ฒ๋ฆฌ ์ํ ๊ฐ๋ฅ
catch(callback)์ ํตํด ์๋ฌ์ฒ๋ฆฌ๊ฐ ๊ฐ๋ฅํ๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
const url = '[https://jsonplaceholder.typicode.com'](https://jsonplaceholder.typicode.com');
const promiseGet = url => {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.send();
xhr.onload = () => {
if (xhr.status === 200) {
// ์๋ต ์ฑ๊ณต
resolve(JSON.parse(xhr.response));
} else {
// ์๋ฌ ์ฒ๋ฆฌ
reject(new Error(xhr.status));
}
};
});
};
promiseGet(\`${url}/xxx/1\`) // ์ ํจํ์ง ์์ url๋ก ์กฐํ ์์ฒญ
.then((user) => console.log(user))
// .then(๋ค๋ฅธ ๋น๋๊ธฐ ์ฒ๋ฆฌ ์ฝ๋) ...
.catch(error => console.error(error));
|
cs |
'๊ฐ๋ฐ ์ง์, ์คํฌ๋ฆฝํธ > JavaScript, TS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Props, Properties (1) | 2023.12.19 |
---|---|
ECMA Script(ES) ๋ฌธ๋ฒ (0) | 2023.12.10 |
Promise ๊ฐ์ฒด์ ์ํ (0) | 2023.12.10 |
Promise (0) | 2023.12.10 |
fetch API (1) | 2023.12.10 |
๋๊ธ