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 ...

Get ECMAScript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.