How to .catch a Promise.reject

Make sure every call to a then() returns a value.

For e.g.

var url = 'https://www.google.co.in';
var options = {};
var resolves = Promise.resolve();

resolves.then(() => {
  console.log('Resolved first promise');
  var fetchPromise = fetch(url, options);
  fetchPromise.then(() => {
    console.log('Completed fetch');
  });
})
.catch(error => {
  console.log('Error', error);
});

Notice the console shows an uncaught exception. However, if you returned the inner promise (or any other value, which ends up turning into a promise via resolve), you end up flattening the promise so exception bubble up.

var url = 'https://www.google.co.in';
var options = {};
var resolves = Promise.resolve();

resolves.then(() => {
  console.log('Resolved first promise');
  var fetchPromise = fetch(url, options);
  return fetchPromise.then(() => {
    console.log('Completed fetch');
  });
})
.catch(error => {
  console.log('Error', error);
});

Notice the exception bubbles up to the outer promise. Hope this clears up things a little bit.


    function test() {
      return new Promise((resolve, reject) => {
        return reject('rejected')
      })
    }

    test().then(function() {
      //here when you resolve
    })
    .catch(function(rej) {
      //here when you reject the promise
      console.log(rej);
    });