λΉλκΈ° νλ‘λ―Έμ€μΌλ μλ¬ μΊμΉ
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' }Β