๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Promise- ํ›„์† ์ฒ˜๋ฆฌ์™€ ์ฒด์ด๋‹(Chaining)

์‹œํ๋ฆฌํ‹ฐ์ง€ํ˜ธ 2023. 12. 10.

๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ์ˆ˜ํ–‰ ๊ฒฐ๊ณผ์— ๋”ฐ๋ฅธ ํ›„์† ์ฒ˜๋ฆฌ

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

๋Œ“๊ธ€