Promise catch MDN code example

Example 1: promise catch

//create a Promise
var p1 = new Promise(function(resolve, reject) {
  resolve("Success");
});

//Execute the body of the promise which call resolve
//So it execute then, inside then there's a throw
//that get capture by catch
p1.then(function(value) {
  console.log(value); // "Success!"
  throw "oh, no!";
}).catch(function(e) {
  console.log(e); // "oh, no!"
});

Example 2: promise catch javascript

// errors that occur in asynchronous code inside promises cannot be caught with regular try/catch
// you need to pass the error handler into `.catch()`

fetchUserData(userId).then(console.log).catch(handleError);