Baseline Widely available
catch()
ë©ìëë Promise
ì¸ì¤í´ì¤ì ë©ìëë¡, íë¡ë¯¸ì¤ê° ê±°ë¶ë ë í¸ì¶ë í¨ì를 ìì½í©ëë¤. ì´ ë©ìëë ì¦ì ë¤ë¥¸ Promise
ê°ì²´ë¥¼ ë°ííì¬ ë¤ë¥¸ íë¡ë¯¸ì¤ ë©ìëë¤ì ì²´ì´ë í ì ìê² í©ëë¤. ì´ë then(undefined, onRejected)
ì ë¨ì¶ ííì
ëë¤.
const promise1 = new Promise((resolve, reject) => {
throw new Error("Uh-oh!");
});
promise1.catch((error) => {
console.error(error);
});
// Expected output: Error: Uh-oh!
구문
promiseInstance.catch(onRejected)
매ê°ë³ì
onRejected
ì´ íë¡ë¯¸ì¤ê° ê±°ë¶ë ë ë¹ë기ì ì¼ë¡ ì¤íë í¨ìì
ëë¤. ì´ í¨ìì ë°í ê°ì catch()
ê° ë°ííë íë¡ë¯¸ì¤ì ì´í ê°ì´ ë©ëë¤. ì´ í¨ìë ë¤ì ì¸ìì í¨ê» í¸ì¶ë©ëë¤.
reason
íë¡ë¯¸ì¤ê° ê±°ë¶ë ê° ì ëë¤.
ìë¡ì´ Promise
를 ë°íí©ëë¤. ì´ ì íë¡ë¯¸ì¤ë ë°í ì íì ë기 ìíì
ëë¤. íì¬ íë¡ë¯¸ì¤ì ìíì ê´ê³ìì´ ë§ì
ëë¤. onRejected
ê° í¸ì¶ëë©´, ë°íë íë¡ë¯¸ì¤ë ì´ í¸ì¶ì ë°í ê°ì ë°ë¼ ì´íëê±°ë, ì´ í¸ì¶ìì ë°ìí ì¤ë¥ë¡ ì¸í´ ê±°ë¶ë©ëë¤. íì¬ íë¡ë¯¸ì¤ê° ì´íëë©´, onRejected
ë í¸ì¶ëì§ ìê³ ë°íë íë¡ë¯¸ì¤ë ëì¼í ê°ì¼ë¡ ì´íë©ëë¤.
catch
ë©ìëë íë¡ë¯¸ì¤ 구ì±ìì ì¤ë¥ ì²ë¦¬ì ì¬ì©ë©ëë¤. Promise
를 ë°íí기 ë문ì, ì매 ë©ìëì¸ then()
ê³¼ ê°ì ë°©ìì¼ë¡ ì²´ì´ëí ì ììµëë¤.
íë¡ë¯¸ì¤ê° ê±°ë¶ëê³ , í¸ì¶í ê±°ë¶ í¸ë¤ë¬ê° ìë ê²½ì°(í¸ë¤ë¬ë then()
, catch()
, ëë finally()
를 íµí´ ì°ê²°ë ì ììµëë¤), ê±°ë¶ ì´ë²¤í¸ë í¸ì¤í¸ì ìí´ íë©´íë©ëë¤. ë¸ë¼ì°ì ììë ì´ë¡ ì¸í´ unhandledrejection
ì´ë²¤í¸ê° ë°ìí©ëë¤. ì´ë¯¸ ì²ë¦¬ëì§ ìì ê±°ë¶ ì´ë²¤í¸ë¥¼ ë°ììí¨ ê±°ë¶ë íë¡ë¯¸ì¤ì í¸ë¤ë¬ê° ì°ê²°ëë©´, ë¤ë¥¸ rejectionhandled
ì´ë²¤í¸ê° ë°ìí©ëë¤.
catch()
ë ë´ë¶ì ì¼ë¡ í¸ì¶ë ê°ì²´ì ëí´ then()
ì í¸ì¶íë©°, undefined
ì onRejected
를 ì¸ìë¡ ì ë¬í©ëë¤. ê·¸ í¸ì¶ì ê°ì´ ì§ì ë°íë©ëë¤. ì´ë ë©ìë를 ê°ì¼ë¤ë©´ ê´ì°°í ì ììµëë¤.
// ì본 Promise.prototype.then/catch ì ì¬ì ìíì¬ ë¡ê·¸ ì½ê°ì ì¶ê°
((Promise) => {
const originalThen = Promise.prototype.then;
const originalCatch = Promise.prototype.catch;
Promise.prototype.then = function (...args) {
console.log("Called .then on %o with arguments: %o", this, args);
return originalThen.apply(this, args);
};
Promise.prototype.catch = function (...args) {
console.error("Called .catch on %o with arguments: %o", this, args);
return originalCatch.apply(this, args);
};
})(Promise);
// ì´ë¯¸ ì´íë íë¡ë¯¸ì¤ì catch í¸ì¶í기
Promise.resolve().catch(function XXX() {});
// Logs:
// Called .catch on Promise{} with arguments: Arguments{1} [0: function XXX()]
// Called .then on Promise{} with arguments: Arguments{2} [0: undefined, 1: function XXX()]
ì´ë undefined
를 ì ë¬íë©´ ì¬ì í ë°íë íë¡ë¯¸ì¤ê° ê±°ë¶ëë©°, ìµì¢
íë¡ë¯¸ì¤ê° ê±°ë¶ëë ê²ì ë°©ì§íë ¤ë©´ í¨ì를 ì ë¬í´ì¼ íë¤ë ê²ì ì미í©ëë¤.
catch()
ë ë¨ìí then()
ì í¸ì¶í기 ë문ì íì í´ëì±ì ì§ìí©ëë¤.
ì°¸ê³ : ìëì ìì ë¤ì Error
ì¸ì¤í´ì¤ë¥¼ ë°ììíµëë¤. ë기ì ì¸ throw
문과 ë§ì°¬ê°ì§ë¡, ì´ë ì¢ì ê´íì¼ë¡ ê°ì£¼ë©ëë¤. ê·¸ë ì§ ìì¼ë©´ ìì¸ë¥¼ ì¡ë ë¶ë¶ìì ì¸ìê° ë¬¸ìì´ì¸ì§ ì¤ë¥ì¸ì§ íì¸íë ìì
ì ìíí´ì¼ íë©°, ì¤í ì¶ì ê³¼ ê°ì ì¤ìí ì 보를 ìì ì ììµëë¤.
const p1 = new Promise((resolve, reject) => {
resolve("Success");
});
p1.then((value) => {
console.log(value); // "Success!"
throw new Error("oh, no!");
})
.catch((e) => {
console.error(e.message); // "oh, no!"
})
.then(
() => console.log("after a catch the chain is restored"), // "after a catch the chain is restored"
() => console.log("Not fired due to the catch"),
);
// ìëë ìì ëì¼íê² ëìí©ëë¤
p1.then((value) => {
console.log(value); // "Success!"
return Promise.reject("oh, no!");
})
.catch((e) => {
console.error(e); // "oh, no!"
})
.then(
() => console.log("after a catch the chain is restored"), // "after a catch the chain is restored"
() => console.log("Not fired due to the catch"),
);
ìë¬ ë°ì ê°ì±
ëë¶ë¶ ì¤ë¥ ë°ì ì catch()
ê° í¸ì¶ë©ëë¤.
const p1 = new Promise((resolve, reject) => {
throw new Error("Uh-oh!");
});
p1.catch((e) => {
console.error(e); // "Uh-oh!"
});
ë¹ë기 í¨ì ë´ì ì¤ë¥ ë°ìì ì²ë¦¬íì§ ëª»í ì¤ë¥ì ê°ì´ íëí©ëë¤.
const p2 = new Promise((resolve, reject) => {
setTimeout(() => {
throw new Error("Uncaught Exception!");
}, 1000);
});
p2.catch((e) => {
console.error(e); // ì ë í¸ì¶ ìë¨
});
resolve
ê° í¸ì¶ë ì´íì ë°ìí ì¤ë¥ë 무ìë©ëë¤.
const p3 = new Promise((resolve, reject) => {
resolve();
throw new Error("Silenced Exception!");
});
p3.catch((e) => {
console.error(e); // ì ë í¸ì¶ ìë¨
});
íë¡ë¯¸ì¤ê° ì´íëìë¤ë©´ catch()ë í¸ì¶ëì§ ìì
// onReject를 í¸ì¶íì§ ìì íë¡ë¯¸ì¤ ìì±
const p1 = Promise.resolve("calling next");
const p2 = p1.catch((reason) => {
// ì ë í¸ì¶ ìë¨
console.error("catch p1!");
console.error(reason);
});
p2.then(
(value) => {
console.log("next promise's onFulfilled");
console.log(value); // ë¤ìì í¸ì¶
},
(reason) => {
console.log("next promise's onRejected");
console.log(reason);
},
);
ëª
ì¸ì ë¸ë¼ì°ì í¸íì± ê°ì´ 보기
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4