⊗jsSpPrmEC 165 of 281 menu

Individual exception catching in promises in JavaScript

In then, you can specify only handler function of an exception by passing null instead of the first parameter:

promise.then( null, function(error) { console.log(error); } );

In this case, it is convenient to use a shortened syntax using the catch method:

promise.catch( function(error) { console.log(error); } );

Rewrite the following code via the catch method:

let promise = new Promise(function(resolve, reject) { setTimeout(function() { let isError = false; if (!isError) { resolve('success'); } else { reject(new Error('error')); } }, 3000); }); promise.then( res => console.log(res), err => console.log(err.message), );
enru