Baseline Widely available
catch()
㯠Promise
ãªãã¸ã§ã¯ãã®ã¡ã½ããã§ããããã¹ãæå¦ãããã¨ãã«å¼ã³åºããã颿°ãã¹ã±ã¸ã¥ã¼ã«ãã¾ããããã¯å³åº§ã«åçã® Promise
ãªãã¸ã§ã¯ããè¿ãã®ã§ãä»ã®ãããã¹ã®ã¡ã½ãããé£é ãã¦å¼ã³åºããã¨ãã§ãã¾ãããã㯠Promise.prototype.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!
æ§æ
catch(onRejected)
catch((reason) => {
// æå¦ãã³ãã©ã¼
})
弿°
onRejected
Promise
ãæå¦ãããæã«å¼ã³åºããã Function
ã§ãããã®é¢æ°ã¯ 1 ã¤ã®å¼æ°ã æå¦ãããçç± ãåãã¾ãã
æ°ãã Promise
ãè¿ãã¾ãããã®æ°ãããããã¹ã¯ãç¾å¨ã®ãããã¹ã®ç¶æ
ã«é¢ä¿ãªããè¿ãã¨ãã«ã¯å¸¸ã«å¾
æ©ç¶æ
ã§ããonRejected
ãã¨ã©ã¼ãçºçãããããããèªèº«ãæå¦ããããããã¹ãè¿ãå ´åãæçµçã«æå¦ããã¾ããããã§ãªããã°ãæçµçã«å±¥è¡ããã¾ãã
catch
ã¡ã½ããã¯è¤åãããããã¹ã®è¤åã®ã¨ã©ã¼å¦çã«ä½¿ç¨ããã¾ãããã㯠Promise
ãè¿ãã®ã§ãå§å¦¹ã¡ã½ããã§ãã then()
ã¨åæ§ã®æ¹æ³ã§é£éãå¯è½ã§ãã
ãããããã¹ãæå¦ãããå¼ã³åºãã¹ãæå¦ãã³ãã©ã¼ããªãå ´åï¼ãã³ãã©ã¼ã¯ then()
, catch()
, finally()
ã®ãããããéãã¦è£
çããã¾ãï¼ãæå¦ã¤ãã³ãã¯ãã¹ããã表é¢åããã¾ãããã©ã¦ã¶ã¼ã§ã¯ããã㯠unhandledrejection
ã¤ãã³ãã¨ãã¦çºçãã¾ãããããæå¦ããããããã¹ã«ãã³ãã©ã¼ãè£
çããããã®æå¦ããã§ã« 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() {});
// ãã°åºå:
// 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"),
() => 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"),
() => 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