
上QQ阅读APP看书,第一时间看更新
How to do it...
- Open your command-line application and navigate to your workspace.
- Create a new folder named 3-03-rejecting-promise-errors.
- Copy or create an index.html that loads and runs a main function from main.js.
- 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'); });
- 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'); });
- 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); });
- Start your Python web server and open the following link in your browser:
http://localhost:8000/. - You should see the following output:
