Promise reject handle

비동기 ν”„λ‘œλ―ΈμŠ€μΌλ•Œ μ—λŸ¬ 캐치

const resolvePromise = new Promise((resolve, reject) => { if(true){ resolve("resolve") }else{ reject("reject") } }) const rejectPromise = new Promise((resolve, reject) => { if(false){ resolve("resolve") }else{ reject("reject") } }) resolvePromise .then((resolve) => { // success handle console.log('resolve',resolve) }) rejectPromise .catch((reject) => { // error handle console.log('reject',reject) })
μœ„μ™€ 같이 비동기일 κ²½μš°λŠ”
resolve λŠ” then, reject λŠ” catch 각각의 μ½œλ°±ν•¨μˆ˜λ‘œ 핸듀링 ν•˜κ²Œ λœλ‹€
비동기 일 κ²½μš°λŠ” μ–΄λ–»κ²Œ 핸듀링 ν•΄μ•Όν•˜λŠ”μ§€
const resolvePromise = new Promise((resolve, reject) => { if(true){ resolve("resolve") }else{ reject("reject") } }) const rejectPromise = new Promise((resolve, reject) => { if(false){ resolve("resolve") }else{ reject("reject") } }) await resolvePromise rejectPromise .catch((reject) => { // error handle console.log('reject',reject) }) const run = async () => { try{ await resolvePromise // resolve }catch(e){ } try{ await rejectPromise }catch(e){ //error handle console.log(e) // reject } } run()
μœ„μ˜ μ½”λ“œμ™€ 같이 async ν•¨μˆ˜μ—μ„œ resolveλŠ” try μ•ˆμ—μ„œ reject λŠ” catch μ•ˆμ—μ„œ λ°˜μ‘ν•˜κ²Œ λœλ‹€
Β 
try ~ catch 없이 rejectκ°€ 싀행될 경우 Promise μ—λŸ¬κ°€ λ°œμƒν•˜κ²Œ λœλ‹€
node:internal/process/promises:246 triggerUncaughtException(err, true /* fromPromise */); ^
[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "reject".] { code: 'ERR_UNHANDLED_REJECTION' }
Β 
λŒ“κΈ€ 0개

λ“±λ‘λœ λŒ“κΈ€μ΄ ν•˜λ‚˜λ„ μ—†μŠ΅λ‹ˆλ‹€...😒