๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Language/Javascript

[Javascript]์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ Callback Hell ํƒˆ์ถœํ•˜๊ธฐ: Promise๋กœ ์ฝ”๋“œ ์ •๋ฆฌํ•˜๊ธฐ!

by YJ Dev 2024. 7. 10.
728x90
๋ฐ˜์‘ํ˜•
SMALL

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ๋น„๋™๊ธฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ๋‹ค๋ฃจ๋‹ค ๋ณด๋ฉด Callback Hell์ด๋ผ๋Š” ๋ฌธ์ œ๋ฅผ ๋งŒ๋‚˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ์ค‘์ฒฉํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ๋กœ, ์ฝ”๋“œ๊ฐ€ ๊นŠ์–ด์ง€๊ณ  ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง€๋ฉฐ ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ์–ด๋ ค์›Œ์ง€๋Š” ์ƒํ™ฉ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ํŠนํžˆ, ์ด์ „ ํฌ์ŠคํŒ…์—์„œ ๋‹ค๋ฃฌ async ํ”„๋กœ๊ทธ๋ž˜๋ฐ๊ณผ ๋ฐ€์ ‘ํ•˜๊ฒŒ ๊ด€๋ จ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ callback hell promise


์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ async ํ”„๋กœ๊ทธ๋ž˜๋ฐ์— ๊ด€ํ•œ ๋‚ด์šฉ์€ ์•„๋ž˜ ํฌ์ŠคํŒ…์„ ์ฐธ๊ณ ํ•ด ์ฃผ์„ธ์š”๐Ÿ˜

""

[Javascript]์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ Async ์™„๋ฒฝ ๊ฐ€์ด๋“œ: ๋น„๋™๊ธฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ๋ชจ๋“  ๊ฒƒ

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ๋น„๋™๊ธฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์€ ๋งค์šฐ ์ค‘์š”ํ•œ ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. ํŠนํžˆ, ๋„คํŠธ์›Œํฌ ์š”์ฒญ, ํŒŒ์ผ ์ฝ๊ธฐ/์“ฐ๊ธฐ ๋“ฑ ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋Š” ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•  ๋•Œ ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝ

creativevista.tistory.com


01. Callback Hell์˜ ์˜ˆ์‹œ๐Ÿ“‰

Callback Hell์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ„๋‹จํ•œ ์˜ˆ์‹œ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์ค‘์ฒฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.

getData(function(result1) {
  processData(result1, function(result2) {
    saveData(result2, function(result3) {
      console.log('All done!');
    });
  });
});

์œ„์™€ ๊ฐ™์€ ์ฝ”๋“œ๋Š” ์ฝ๊ธฐ ์–ด๋ ต๊ณ , ์—๋Ÿฌ ์ฒ˜๋ฆฌ๊ฐ€ ๋ณต์žกํ•ด์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ Callback Hell์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค.


02. Promise์˜ ๋“ฑ์žฅโœจ

Promise๋Š” ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์— ๋„์ž…๋œ ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค. Promise๋Š” ๋น„๋™๊ธฐ ์ž‘์—…์„ ๋” ์‰ฝ๊ฒŒ ๊ด€๋ฆฌํ•˜๊ณ , ๊ฐ€๋…์„ฑ์„ ๋†’์—ฌ์ค๋‹ˆ๋‹ค. ๊ฐ„๋‹จํžˆ ๋งํ•ด, Promise๋Š” ๋น„๋™๊ธฐ ์ž‘์—…์˜ ์„ฑ๊ณต ๋˜๋Š” ์‹คํŒจ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.


03. Promise ์‚ฌ์šฉ๋ฒ• ๊ธฐ์ดˆ ๐Ÿ› ๏ธ

Promise๋Š” new Promise ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์ƒ์„ฑ์ž๋Š” ๋‘ ๊ฐœ์˜ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์ธ์ˆ˜๋กœ ๋ฐ›์Šต๋‹ˆ๋‹ค. resolve์™€ reject ๋น„๋™๊ธฐ ์ž‘์—…์ด ์„ฑ๊ณตํ•˜๋ฉด resolve๋ฅผ ํ˜ธ์ถœํ•˜๊ณ , ์‹คํŒจํ•˜๋ฉด reject๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.

new Promise((resolve, reject) => {
  // ๋น„๋™๊ธฐ ์ž‘์—… ์ˆ˜ํ–‰
  if (์„ฑ๊ณต) {
    resolve(result); // ์„ฑ๊ณต ์‹œ ํ˜ธ์ถœ
  } else {
    reject(error); // ์‹คํŒจ ์‹œ ํ˜ธ์ถœ
  }
});

Promise๋Š” .then(), .catch(), .finally() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋น„๋™๊ธฐ ์ž‘์—…์˜ ๊ฒฐ๊ณผ๋ฅผ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.


04. Callback Hell์„ Promise๋กœ ๋ฐ”๊พธ๊ธฐ๐Ÿ”„

์•ž์„œ ๋ณธ Callback Hell ์˜ˆ์ œ๋ฅผ Promise๋กœ ๋ณ€ํ™˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

getData()
  .then(result1 => processData(result1))
  .then(result2 => saveData(result2))
  .then(result3 => console.log('All done!'))
  .catch(error => console.error(error));

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ฝ”๋“œ๊ฐ€ ํ›จ์”ฌ ๊น”๋”ํ•ด์ง€๊ณ , ๊ฐ ๋‹จ๊ณ„์—์„œ์˜ ์—๋Ÿฌ ์ฒ˜๋ฆฌ๊ฐ€ ์šฉ์ดํ•ด์ง‘๋‹ˆ๋‹ค.


05. Promise Chaining๐ŸŒŸ

Promise Chaining์€ ์—ฌ๋Ÿฌ ๋น„๋™๊ธฐ ์ž‘์—…์„ ์ˆœ์ฐจ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•  ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ then์ด ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฐ’์€ ๋‹ค์Œ then์œผ๋กœ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค.

fetch('https://api.example.com/data')
  .then(response => response.json())
  .then(data => {
    console.log(data);
    return processData(data);
  })
  .then(processedData => saveData(processedData))
  .catch(error => console.error('Error:', error));

์ด์ฒ˜๋Ÿผ Promise Chaining์„ ํ†ตํ•ด ๋น„๋™๊ธฐ ์ž‘์—…์„ ์ˆœ์ฐจ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ณ , ๊ฐ€๋…์„ฑ์„ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


06. Promise.all๊ณผ Promise.race๐Ÿ’ซ

  • Promise.all: ์—ฌ๋Ÿฌ ๊ฐœ์˜ Promise๋ฅผ ๋ณ‘๋ ฌ๋กœ ์ฒ˜๋ฆฌํ•˜๊ณ , ๋ชจ๋“  Promise๊ฐ€ ํ•ด๊ฒฐ๋˜์—ˆ์„ ๋•Œ ํ•˜๋‚˜์˜ Promise๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ํ•˜๋‚˜๋ผ๋„ ์‹คํŒจํ•˜๋ฉด, ์‹คํŒจํ•œ Promise๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • Promise.race: ์—ฌ๋Ÿฌ ๊ฐœ์˜ Promise ์ค‘ ๊ฐ€์žฅ ๋จผ์ € ํ•ด๊ฒฐ๋˜๊ฑฐ๋‚˜ ๊ฑฐ๋ถ€๋œ Promise๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
let promise1 = new Promise((resolve) => setTimeout(resolve, 100, "์ฒซ ๋ฒˆ์งธ"));
let promise2 = new Promise((resolve) => setTimeout(resolve, 200, "๋‘ ๋ฒˆ์งธ"));
let promise3 = new Promise((resolve, reject) => setTimeout(reject, 150, "์„ธ ๋ฒˆ์งธ ์‹คํŒจ"));

Promise.all([promise1, promise2])
    .then((results) => {
        console.log(results); // ["์ฒซ ๋ฒˆ์งธ", "๋‘ ๋ฒˆ์งธ"]
    })
    .catch((error) => {
        console.error(error);
    });

Promise.race([promise1, promise2, promise3])
    .then((result) => {
        console.log(result); // "์ฒซ ๋ฒˆ์งธ"
    })
    .catch((error) => {
        console.error(error); // "์„ธ ๋ฒˆ์งธ ์‹คํŒจ"
    });

07. ์—๋Ÿฌ ํ•ธ๋“ค๋ง๐Ÿ’ฅ

Promise์—์„œ ์—๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. .catch() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์—๋Ÿฌ๋ฅผ ์žก์•„๋‚ด๊ณ  ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

doSomething()
  .then(result => doSomethingElse(result))
  .catch(error => console.error('Something went wrong:', error));

Callback Hell์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋น„๋™๊ธฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ํ”ํžˆ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด Promise๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ๊ณผ ์œ ์ง€๋ณด์ˆ˜์„ฑ์ด ํฌ๊ฒŒ ํ–ฅ์ƒ๋ฉ๋‹ˆ๋‹ค. Promise๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋น„๋™๊ธฐ ์ž‘์—…์„ ๊ด€๋ฆฌํ•˜๊ณ , chaining๊ณผ ์—๋Ÿฌ ํ•ธ๋“ค๋ง์„ ํ†ตํ•ด ๋” ์•ˆ์ „ํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•