const promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("완료");
}, 1000);
});
promise
.then(result => {
console.log(result); // 출력: 완료
throw new Error("에러발생"); // 에러 던짐
})
.catch(error => {
console.log(error.message); // 출력: 에러발생
return "복구됨"; // 에러 복구 → 다음 then으로 전달됨
})
.then(result => {
console.log(result); // 출력: 복구됨
});
1️⃣ new Promise(...):
2️⃣ .then(result => { ... }):
3️⃣ .catch(error => { ... }):
4️⃣ .then(result => { ... }):
완료
에러발생
복구됨
resolve("완료")
↓
.then → 출력 후 에러 발생
↓
.catch → 에러 메시지 출력, 복구 문자열 리턴
↓
.then → 복구 결과 출력