How to do it...

  1. Open your command-line application and navigate to your workspace.
  2. Create a new folder named 3-03-rejecting-promise-errors.
  3. Copy or create an index.html that loads and runs a main function from main.js.
  4. Create a main.js file that creates a promise, and logs messages before and after the promise is created and when the promise is fulfilled:
   new Promise(function (resolve) { 
     resolve(); 
      }).then(function (result) { 
     console.log('Promise Completed'); 
   }); 
  1. Add a second argument to the promise callback named reject, and call reject with a new error:
    new Promise(function (resolve, reject) { 
      reject(new Error('Something went wrong'); 
    }).then(function (result) { 
    console.log('Promise Completed'); 
   }); 
  1. Chain a catch call off the promise. Pass a function that logs out its only argument:
    new Promise(function (resolve, reject) { 
      reject(new Error('Something went wrong'); 
     }).then(function (result) { 
     console.log('Promise Completed'); 
    }).catch(function (error) {      console.error(error);    });
  1. Start your Python web server and open the following link in your browser: 
    http://localhost:8000/.
  2. You should see the following output: