ํ๋ผ๋ฏธ์ค๋ ์ฑ๊ณต ๋๋ ์คํจ๋ง ํ๋ค.
exeucutor๋ ํ๋ผ๋ฏธ์ค์ ์ํ๋ฅผ ์ดํ(fulfilled) ๋๋ ๊ฑฐ๋ถ(rejected)๋ก ๋ณ๊ฒฝ์ํจ๋ค.
์ฑ๊ณต ํน์ ์คํจ๋ง ํ๋ค. resolve๋ reject๊ฐ ๋์ด ์ํ๊ฐ ๊ฒฐ์ ๋๋ฉด, ์ดํ๋ก๋ ์ํ๋ฅผ ๋ณ๊ฒฝํ ์ ์๋ค. ์ฒ๋ฆฌ๊ฐ ๋๋ ํ๋ผ๋ฏธ์ค์ resolve๋ reject๊ฐ ํธ์ถ๋๋ฉด ๋ฌด์๋๋ค.
Copy 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๋ฅผ ์ฆ์ ํธ์ถํด๋ ๋๋ค.
Copy 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์ ์๋ฌ๋ฅผ ๋ฐ๋๋ค.
Copy promise.then(
function (result) {
/* ๊ฒฐ๊ณผ(result)๋ฅผ ๋ค๋ฃน๋๋ค */
},
function (error) {
/* ์๋ฌ(error)๋ฅผ ๋ค๋ฃน๋๋ค */
}
)
Copy 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์ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ํ์ธํ ์ ์๋ค.
Copy Promise.reject(new Error("error instance")).catch((e) => console.error(e))
Promise.reject("just string error").catch((e) => console.error(e))
Copy 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๊ฐ ์ค์ ํ ํจ์๊ฐ ๋ฐํํ๋ ํ๋ผ๋ฏธ์ค๋ฅผ ๋ฐํํ๋ค.
Copy 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)
})
Copy Experiment completed
Mail has arrived
Copy 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