Promise

new Promise

  • Promise๋Š” class์ด๊ธฐ์— new ํ‚ค์›Œ๋“œ๋ฅผ ํ†ตํ•ด Promise ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ

  • Promise๋Š” ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•  ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์ธ์ˆ˜๋กœ ๋ฐ›๋Š”๋ฐ, ์ด ์ฝœ๋ฐฑํ•จ์ˆ˜๋Š” resolve, reject ํ•จ์ˆ˜๋ฅผ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์Œ

  • Promise ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๋ฉด ์ฝœ๋ฐฑํ•จ์ˆ˜๋Š” ์ž๋™์œผ๋กœ ์‹คํ–‰๋จ

  • ์ฝ”๋“œ๊ฐ€ ์ž˜ ์ฒ˜๋ฆฌ๋˜์—ˆ๋‹ค๋ฉด resolveํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ณ , ์—๋Ÿฌ ๋ฐœ์ƒ ์‹œ rejectํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ

    let promise = new Promise((resove, reject) => {
        // 1. ์ •์ƒ์ ์œผ๋กœ ์ฒ˜๋ฆฌ๋˜๋Š” ๊ฒฝ์šฐ resolve์˜ ์ธ์ž์— ๊ฐ’ ์ „๋‹ฌ
        resolve(value);
    
        // 2. ์—๋Ÿฌ ๋ฐœ์ƒ์‹œ reject์˜ ์ธ์ž์— ์—๋Ÿฌ๋ฉ”์‹œ์ง€ ์ „๋‹ฌ
        reject(value);
    })

Promise ๊ฐ์ฒด์˜ ๋‚ด๋ถ€ ํ”„๋กœํผํ‹ฐ

  • new Promise๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” Promise ๊ฐ์ฒด๋Š” state, result ๋‚ด๋ถ€ ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์ง

  • ๋‹ค๋งŒ, ์ง์ ‘ ์ ‘๊ทผํ•  ์ˆ˜ ์—†๊ณ  .then, .catch, .finally ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉํ•ด ์ ‘๊ทผ ๊ฐ€๋Šฅ

  • State

    • ๊ธฐ๋ณธ ์ƒํƒœ๋Š” pending. ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•  ์ฝœ๋ฐฑํ•จ์ˆ˜๊ฐ€ ์ž˜ ์ž‘๋™ํ–ˆ๋‹ค๋ฉด โ†’ fulfilled๋กœ ๋ณ€๊ฒฝ, ์—๋Ÿฌ ๋ฐœ์ƒ ์‹œ rejected

  • Result

    • ๊ธฐ๋ณธ ์ƒํƒœ๋Š” undefined ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•  ์ฝœ๋ฐฑํ•จ์ˆ˜๊ฐ€ ์ž˜ ์ž‘๋™ํ•˜์—ฌ resolve(value) ํ˜ธ์ถœ๋˜๋ฉด โ†’ value๋กœ, ์—๋Ÿฌ ๋ฐœ์ƒํ•ด reject(error) ํ˜ธ์ถœ๋˜๋ฉด โ†’ error ๋กœ ๋ณ€๊ฒฝ

then, catch, finally

then

  • ์ฝœ๋ฐฑํ•จ์ˆ˜์— ์ž‘์„ฑํ–ˆ๋˜ ์ฝ”๋“œ๋“ค์ด ์ž˜ ์ฒ˜๋ฆฌ๋˜์—ˆ๋‹ค๋ฉด resolve ํ•จ์ˆ˜ ํ˜ธ์ถœ โ†’ .then ๋ฉ”์†Œ๋“œ๋กœ ์ ‘๊ทผ ๊ฐ€๋Šฅ

  • .then ์•ˆ์—์„œ ๋ฆฌํ„ดํ•œ ๊ฐ’์ด Promise๋ผ๋ฉด Promise ๋‚ด๋ถ€ ํ”„๋กœํผํ‹ฐ result๋ฅผ ๋‹ค์Œ .then์˜ ์ฝœ๋ฐฑํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ๋ฐ›์•„์˜ด

  • Promise๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ๋ฆฌํ„ดํ•œ ๊ฐ’์„ .then์˜ ์ฝœ๋ฐฑํ•จ์ˆ˜ ์ธ์ž๋กœ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ์Œ

    let promise = new Promise((resolve, reject) => {
        resolve("์„ฑ๊ณต");
    });
    
    promise.then(value => {
        console.log(value); // "์„ฑ๊ณต"
    })

Catch

  • ์ฝœ๋ฐฑํ•จ์ˆ˜์— ์ž‘์„ฑํ•œ ์ฝ”๋“œ์˜ ์—๋Ÿฌ ๋ฐœ์ƒ ์‹œ rejectํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  .catch ๋ฉ”์†Œ๋“œ๋กœ ์ ‘๊ทผ ๊ฐ€๋Šฅ

    let promise = new Promise(function(resolve, reject) {
    reject(new Error("์—๋Ÿฌ"))
    });
    
    promise.catch(error => {
    console.log(error); // Error: ์—๋Ÿฌ
    })

Finally

  • ์ฝœ๋ฐฑํ•จ์ˆ˜์— ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋“ค์ด ์ •์ƒ ์ฒ˜๋ฆฌ ์—ฌ๋ถ€์™€ ๊ด€๊ณ„ ์—†์ด .finally ๋ฉ”์†Œ๋“œ๋กœ ์ ‘๊ทผ ๊ฐ€๋Šฅ

    let promise = new Promise(function(resolve, reject) {
      resolve("์„ฑ๊ณต");
    });
    
    promise
    .then(value => {
      console.log(value); // "์„ฑ๊ณต"
    })
    .catch(error => {
      console.log(error);
    })
    .finally(() => {
      console.log("์„ฑ๊ณต์ด๋“  ์‹คํŒจ๋“  ์ž‘๋™"); // "์„ฑ๊ณต์ด๋“  ์‹คํŒจ๋“  ์ž‘๋™"
    })

Promise Chaining

  • ๋น„๋™๊ธฐ ์ž‘์—…์„ ์ˆœ์ฐจ์ ์œผ๋กœ ์ง„ํ–‰ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ

  • Promise all()

  • ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋น„๋™๊ธฐ ์ž‘์—…์„ ๋™์‹œ์— ์ฒ˜๋ฆฌํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ

  • ์ธ์ž๋กœ๋Š” ๋ฐฐ์—ด์„ ๋ฐ›์œผ๋ฉฐ, ํ•ด๋‹น ๋ฐฐ์—ด์— ์žˆ๋Š” ๋ชจ๋“  Promise์—์„œ ์ฝœ๋ฐฑํ•จ์ˆ˜ ๋‚ด ์ž‘์„ฑํ–ˆ๋˜ ์ฝ”๋“œ๋“ค์ด ์ •์ƒ์ ์œผ๋กœ ์ฒ˜๋ฆฌ๋˜์—ˆ๋‹ค๋ฉด ๊ฒฐ๊ณผ๋ฅผ ๋ฐฐ์—ด์— ์ €์žฅํ•ด ์ƒˆ๋กœ์šด Promise๋ฅผ ๋ฐ˜ํ™˜

  • ๋”๋ถˆ์–ด, ์ธ์ž๋กœ ๋ฐ›์€ ๋ฐฐ์—ด์˜ Promise ์ค‘ ํ•˜๋‚˜๋ผ๋„ ์—๋Ÿฌ ๋ฐœ์ƒ ์‹œ, ๋‚˜๋จธ์ง€ Promise state์™€ ์ƒ๊ด€์—†์ด ์ฆ‰์‹œ ์ข…๋ฃŒ

    Promise.all([promiseOne(), promiseTwo(), promiseThree()])
        .then((value) => console.log(value)) // ['1์ดˆ', '2์ดˆ', '3์ดˆ']
        .catch((err) => console.log(err));

Last updated