--- title: Promise.prototype.catch() slug: Web/JavaScript/Reference/Global_Objects/Promise/catch tags: - ECMAScript6 - Method - Promise - Prototype - Referensi translation_of: Web/JavaScript/Reference/Global_Objects/Promise/catch ---
Method catch() mengembalikan Promise dan hanya setuju jika kasusnya gagal. Sama halnya dengan memenggil method {{jsxref("Promise.then", "Promise.prototype.then(undefined, onRejected)")}}.
p.catch(onRejected);
p.catch(function(reason) {
// rejection
});
Promise ditolak. Fungsi ini memiliki satu argumen, alasan penolakan.Method catch sangat berguna untuk menangani error di gabungan promis anda.
catchvar p1 = new Promise(function(resolve, reject) {
resolve('Success');
});
p1.then(function(value) {
console.log(value); // "Success!"
throw 'oh, no!';
}).catch(function(e) {
console.log(e); // "oh, no!"
}).then(function(){
console.log('after a catch the chain is restored');
}, function () {
console.log('Not fired due to the catch');
});
// The following behaves the same as above
p1.then(function(value) {
console.log(value); // "Success!"
return Promise.reject('oh, no!');
}).catch(function(e) {
console.log(e); // "oh, no!"
}).then(function(){
console.log('after a catch the chain is restored');
}, function () {
console.log('Not fired due to the catch');
});
var p1 = new Promise(function(resolve, reject) {
throw 'Uh-oh!';
});
p1.catch(function(e) {
console.log(e); // "Uh-oh!"
});
var p2 = new Promise(function(resolve, reject) {
setTimeout(function() {
throw 'Uncaught Exception!';
}, 1000);
});
p2.catch(function(e) {
console.log(e); // This is never called
});
| Spesifikasi | Status | Comment |
|---|---|---|
| {{SpecName('ES6', '#sec-promise.prototype.catch', 'Promise.prototype.catch')}} | {{Spec2('ES6')}} | Initial definition in an ECMA standard. |
| {{SpecName('ESDraft', '#sec-promise.prototype.catch', 'Promise.prototype.catch')}} | {{Spec2('ESDraft')}} |
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
{{Compat("javascript.builtins.Promise.catch")}}