promise
ํ๋ผ๋ฏธ์ค๋ ์ฑ๊ณต ๋๋ ์คํจ๋ง ํ๋ค.
exeucutor๋ ํ๋ผ๋ฏธ์ค์ ์ํ๋ฅผ ์ดํ(fulfilled) ๋๋ ๊ฑฐ๋ถ(rejected)๋ก ๋ณ๊ฒฝ์ํจ๋ค.
์ฑ๊ณต ํน์ ์คํจ๋ง ํ๋ค. resolve๋ reject๊ฐ ๋์ด ์ํ๊ฐ ๊ฒฐ์ ๋๋ฉด, ์ดํ๋ก๋ ์ํ๋ฅผ ๋ณ๊ฒฝํ ์ ์๋ค. ์ฒ๋ฆฌ๊ฐ ๋๋ ํ๋ผ๋ฏธ์ค์ resolve๋ reject๊ฐ ํธ์ถ๋๋ฉด ๋ฌด์๋๋ค.
resolve, reject ํจ์ ์ฆ์ ํธ์ถํ๊ธฐ
executor์์ ๊ผญ ๋น๋๊ธฐ ์ฐ์ฐ์ ์ํํ ํ, resolve๋ reject๋ฅผ ํธ์ถํ์ง ์์๋ ๋๋ค. resolve๋ reject๋ฅผ ์ฆ์ ํธ์ถํด๋ ๋๋ค.
์๋น์ : then, catch, finally
ํ๋ผ๋ฏธ์ค ๊ฐ์ฒด : executor์ ์๋น ํจ์๋ฅผ ์ด์ด์ฃผ๋ ์ญํ
์๋นํจ์๋ .then, .catch, .finally ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๋ฑ๋ก
then
๋ ๊ฐ์ ์ฝ๋ฐฑ ํจ์๋ฅผ ์ธ์๋ก ๋ฐ๋๋ค.
์ฒซ ๋ฒ์งธ ์ฝ๋ฐฑ์ ์ดํ๋์์ ๋ ์คํ๋๋ค. resolve์ ๊ฐ์ ๋ฐ๋๋ค.
๋ ๋ฒ์งธ ์ฝ๋ฐฑ์ ๊ฑฐ๋ถ๋์์ ๋ ์คํ๋๋ค. reject์ ์๋ฌ๋ฅผ ๋ฐ๋๋ค.
์์
catch
๋ด๋ถ์ ์ผ๋ก
.then(undefined, onRejectedFunction)
์ ํธ์ถํ๋ค.catch๋ ํ๋ผ๋ฏธ์ค๋ฅผ ๋ฐํํ๋ค.
onRejected ํธ๋ค๋ฌ ์์์
throws
๊ฐ ์๊ฑฐ๋,Promise.rejected
๋ฅผ ๋ฐํํ๋ฉด catch๊ฐ ๋ฐํํ๋ ํ๋ผ๋ฏธ์ค๋ reject๋๋ค.
catch์์ Error ๊ฐ์ฒด ๋ฐํํ๊ธฐ
catch์์ ๋จ์ํ string์ throwingํ๋ ๊ฒ ๋ณด๋ค, Error ์ธ์คํด์ค๋ฅผ throwingํ๋๊ฒ ๋ ์ข๋ค. Error ์ธ์คํด์ค๋ฅผ ๋ฐํํด์ผ stack trace์ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ํ์ธํ ์ ์๋ค.
finally
์ธ์๋ฅผ ๋ฐ์ง ์๋๋ค. ํ๋ผ๋ฏธ์ค๊ฐ ์ดํ๋ ๊ฒ์ธ์ง, ๊ฑฐ๋ถ๋ ๊ฒ์ธ์ง ํ์ธํ ์ ์๋ ํ์คํ ๋ฐฉ๋ฒ์ด ์๊ธฐ ๋๋ฌธ์ด๋ค.
ํ๋ผ๋ฏธ์ค๊ฐ ์ดํ ๋๋ ๊ฑฐ๋ถ๋๋ฉด, finally์ ์ฝ๋ฐฑ์ด ์คํ๋๋ค.
ํ๋ผ๋ฏธ์ค์ ๊ฒฐ๊ณผ์ ์๊ด์์ด ์คํ๋์ด์ผํ๋ cleanupํจ์๋ ์ฐ์ฐ์ ํด์ผํ ๋ finally๋ฅผ ์ฌ์ฉํ๋ฉด ์ข๋ค.
finally๊ฐ ์ค์ ํ ํจ์๊ฐ ๋ฐํํ๋ ํ๋ผ๋ฏธ์ค๋ฅผ ๋ฐํํ๋ค.
Last updated