promise

ํ”„๋ผ๋ฏธ์Šค๋Š” ์„ฑ๊ณต ๋˜๋Š” ์‹คํŒจ๋งŒ ํ•œ๋‹ค.

exeucutor๋Š” ํ”„๋ผ๋ฏธ์Šค์˜ ์ƒํƒœ๋ฅผ ์ดํ–‰(fulfilled) ๋˜๋Š” ๊ฑฐ๋ถ€(rejected)๋กœ ๋ณ€๊ฒฝ์‹œํ‚จ๋‹ค.

์„ฑ๊ณต ํ˜น์€ ์‹คํŒจ๋งŒ ํ•œ๋‹ค. resolve๋‚˜ reject๊ฐ€ ๋˜์–ด ์ƒํƒœ๊ฐ€ ๊ฒฐ์ •๋˜๋ฉด, ์ดํ›„๋กœ๋Š” ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค. ์ฒ˜๋ฆฌ๊ฐ€ ๋๋‚œ ํ”„๋ผ๋ฏธ์Šค์— resolve๋‚˜ reject๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด ๋ฌด์‹œ๋œ๋‹ค.

const testMultipleResolve = new Promise((resolve, reject) => {
  resolve("done")
  reject(new Error("error")) // ๋ฌด์‹œ
  setTimeout(() => reject("error2"), 10) // ๋ฌด์‹œ
})
;(async () => {
  try {
    console.log(await testMultipleResolve) // 'done'
  } catch (e) {
    console.log(e)
  }
})()

resolve, reject ํ•จ์ˆ˜ ์ฆ‰์‹œ ํ˜ธ์ถœํ•˜๊ธฐ

executor์—์„œ ๊ผญ ๋น„๋™๊ธฐ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•œ ํ›„, resolve๋‚˜ reject๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค. resolve๋‚˜ reject๋ฅผ ์ฆ‰์‹œ ํ˜ธ์ถœํ•ด๋„ ๋œ๋‹ค.

const immediatePromise = new Promise((resolve, reject) => {
  resolve("done immediately")
})
;(() => {
  console.log(promise) // Promise { <pending> }
  console.log(immediatePromise) // Promise { 'done immediately' }
})()

์†Œ๋น„์ž : then, catch, finally

  • ํ”„๋ผ๋ฏธ์Šค ๊ฐ์ฒด : executor์™€ ์†Œ๋น„ ํ•จ์ˆ˜๋ฅผ ์ด์–ด์ฃผ๋Š” ์—ญํ• 

  • ์†Œ๋น„ํ•จ์ˆ˜๋Š” .then, .catch, .finally ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋“ฑ๋ก

then

  • ๋‘ ๊ฐœ์˜ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์ธ์ˆ˜๋กœ ๋ฐ›๋Š”๋‹ค.

  • ์ฒซ ๋ฒˆ์งธ ์ฝœ๋ฐฑ์€ ์ดํ–‰๋˜์—ˆ์„ ๋•Œ ์‹คํ–‰๋œ๋‹ค. resolve์˜ ๊ฐ’์„ ๋ฐ›๋Š”๋‹ค.

  • ๋‘ ๋ฒˆ์งธ ์ฝœ๋ฐฑ์€ ๊ฑฐ๋ถ€๋˜์—ˆ์„ ๋•Œ ์‹คํ–‰๋œ๋‹ค. reject์˜ ์—๋Ÿฌ๋ฅผ ๋ฐ›๋Š”๋‹ค.

promise.then(
  function (result) {
    /* ๊ฒฐ๊ณผ(result)๋ฅผ ๋‹ค๋ฃน๋‹ˆ๋‹ค */
  },
  function (error) {
    /* ์—๋Ÿฌ(error)๋ฅผ ๋‹ค๋ฃน๋‹ˆ๋‹ค */
  }
)
  • ์˜ˆ์ œ

const promise = new Promise((resolve, reject) => {
  setTimeout(() => resolve("done"), 10)
})
const rPromise = new Promise((resolve, reject) => {
  setTimeout(() => reject("error"), 10)
})

;(async () => {
  promise.then(
    (result) => console.log(`result1 : ${result}`), // result1 : done
    (error) => console.error(`reject1 ${error}`) // ์‹คํ–‰๋˜์ง€ ์•Š์Œ
  )
  rPromise.then(
    (result) => console.log(`result2 : ${result}`), // ์‹คํ–‰๋˜์ง€ ์•Š์Œ
    (error) => console.error(`reject2 ${error}`) // reject2 error
  )
})()

catch

  • ๋‚ด๋ถ€์ ์œผ๋กœ .then(undefined, onRejectedFunction)์„ ํ˜ธ์ถœํ•œ๋‹ค.

  • catch๋Š” ํ”„๋ผ๋ฏธ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

    • onRejected ํ•ธ๋“ค๋Ÿฌ ์•ˆ์—์„œ throws๊ฐ€ ์žˆ๊ฑฐ๋‚˜, Promise.rejected๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด catch๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ”„๋ผ๋ฏธ์Šค๋Š” reject๋œ๋‹ค.

catch์—์„œ Error ๊ฐ์ฒด ๋ฐ˜ํ™˜ํ•˜๊ธฐ

catch์—์„œ ๋‹จ์ˆœํžˆ string์„ throwingํ•˜๋Š” ๊ฒƒ ๋ณด๋‹ค, Error ์ธ์Šคํ„ด์Šค๋ฅผ throwingํ•˜๋Š”๊ฒŒ ๋” ์ข‹๋‹ค. Error ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ stack trace์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

Promise.reject(new Error("error instance")).catch((e) => console.error(e))
Promise.reject("just string error").catch((e) => console.error(e))
Error: error instance
    at Object.<anonymous> (/Users/kakao/study/js/promise.js:79:16)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47
just string error

finally

  • ์ธ์ž๋ฅผ ๋ฐ›์ง€ ์•Š๋Š”๋‹ค. ํ”„๋ผ๋ฏธ์Šค๊ฐ€ ์ดํ–‰๋œ ๊ฒƒ์ธ์ง€, ๊ฑฐ๋ถ€๋œ ๊ฒƒ์ธ์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ํ™•์‹คํ•œ ๋ฐฉ๋ฒ•์ด ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

  • ํ”„๋ผ๋ฏธ์Šค๊ฐ€ ์ดํ–‰ ๋˜๋Š” ๊ฑฐ๋ถ€๋˜๋ฉด, finally์˜ ์ฝœ๋ฐฑ์ด ์‹คํ–‰๋œ๋‹ค.

  • ํ”„๋ผ๋ฏธ์Šค์˜ ๊ฒฐ๊ณผ์— ์ƒ๊ด€์—†์ด ์‹คํ–‰๋˜์–ด์•ผํ•˜๋Š” cleanupํ•จ์ˆ˜๋‚˜ ์—ฐ์‚ฐ์„ ํ•ด์•ผํ•  ๋•Œ finally๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹๋‹ค.

  • finally๊ฐ€ ์„ค์ •ํ•œ ํ•จ์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ”„๋ผ๋ฏธ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

function checkMail() {
  return new Promise((resolve, reject) => {
    if (Math.random() > 0.5) {
      resolve("Mail has arrived")
    } else {
      reject(new Error("Failed to arrive"))
    }
  })
}

checkMail()
  .finally(() => {
    console.log("Experiment completed")
  })
  .then((mail) => {
    console.log(mail)
  })
  .catch((err) => {
    console.error(err)
  })
Experiment completed
Mail has arrived
Experiment completed
Error: Failed to arrive
    at /Users/kakao/study/js/promise.js:95:14
    at new Promise (<anonymous>)
    at checkMail (/Users/kakao/study/js/pro

Last updated