Page 64 - Nodejs 교과서 개정2판
P. 64
resolve('성공');
} else {
reject('실패');
}
});
// 다른 코드가 들어갈 수 있음
promise
.then((message) => {
console.log(message); // 성공(resolve)한 경우 실행
})
.catch((error) => {
console.error(error); // 실패(reject)한 경우 실행
})
.finally(() => { // 끝나고 무조건 실행
console.log('무조건');
});
new Promise۽ ۽झܳ ࢤࢿೡ ࣻ ਵݴ Ӓ ղࠗী resolve৬ rejectܳ ݒѐ߸ࣻ۽ ыח ߔ ೣࣻܳ ֍
णפ ۧѱ ݅ٚ promise ߸ࣻী thenҗ catch ݫࢲ٘ܳ ࠢੌ ࣻ णפ ۽झ ղࠗীࢲ resolveо
ഐغݶ then प೯غҊ rejectо ഐغݶ catchо प೯ؾפ finally ࠗ࠙ ࢿҕ पಁ ৈࠗ৬ ࢚ҙহ
प೯ؾפ
resolve৬ rejectী ֍যળ ੋࣻח пп thenҗ catch ݒѐ߸ࣻীࢲ ߉ਸ ࣻ णפ resolve('성공'
) ഐغݶ then messageо '성공' ؾפ ݅ড reject('실패')о ഐغݶ DBUDI errorо '실패'
о غח Ѫੑפ condition ߸ࣻܳ false۽ ߄Լࠁݶ catchীࢲ ী۞о ۽Ӧؾפ
۽झܳ औѱ ࢸݺೞݶ प೯ ߄۽ ೞغ ѾҞч աী ߉ח ёੑפ ѾҞч प೯ ৮ܐػ റ t
henա catch ݫࢲ٘ܳ ా೧ ߉णפ ਤ ઁীࢲח new Promise৬ promise.then ࢎী ܲ ٘о ٜয
т ࣻب णפ new Promiseח ߄۽ प೯غ݅ ѾҞч thenਸ ࠢਸ ٸ ߉ѱ ؾפ
thenա catchীࢲ द ܲ UIFOա catchܳ ࠢੌ ࣻ णפ then return чਸ then ݒ
ѐ߸ࣻ۽ ֈӤפ ۽झܳ returnೠ ҃ীח ۽झо ࣻ೯ػ റ UIFOա catchо ഐؾפ
promise
.then((message) => {
return new Promise((resolve, reject) => {
resolve(message);
});
})
.then((message2) => {
console.log(message2);