
上QQ阅读APP看书,第一时间看更新
How to do it...
- Open your command-line application and navigate to your workspace.
- Create a new folder named 3-07-handle-errors-promise-catch.
- Copy or create an index.html that loads and runs a main function from main.js.
- Create a main.js file with a main function that creates an object named rocket:
export function main() { console.log('Before promise created'); const rocket = {}; console.log('After promise created'); }
- Create a function addBoosters that throws an error:
function addBoosters (rocket) { throw new Error('Unable to add Boosters'); }
- Create a function performGuidanceDiagnostic that returns a promise that rejects an error:
function performGuidanceDiagnostic (rocket) { return new Promise(function (resolve, reject) { reject(new Error('Unable to finish guidance diagnostic')); }); }
- Use Promise.resolve to pass the rocket object to these functions, and chain a catch off each of them:
export function main() { console.log('Before promise created'); const rocket = {}; Promise.resolve(rocket).then(addBoosters)
.catch(console.error);
Promise.resolve(rocket).then(performGuidanceDiagnostic)
.catch(console.error); console.log('After promise created'); }
- Start your Python web server and open the following link in your browser:
http://localhost:8000/.
- You should see the following output:
