97. Consuming Promises
To handle the result of a Promise, we use the .then(), .catch(), and .finally() methods.
1. .then() (Handling Success)
Attached to a Promise to handle the successful (fulfilled) outcome.
2. .catch() (Handling Error)
Attached to handle the rejected (failed) outcome.
Example: Consumption
Using the myPromise from Lesson 96:
javascript myPromise .then(result => { // This runs if resolve() was called console.log('Success:', result); }) .catch(error => { // This runs if reject() was called console.error('Error occurred:', error.message); });
console.log('Program continues while Promise is pending...');
3. .finally()
This block runs regardless of whether the promise was resolved or rejected, often used for cleanup tasks (like hiding a loading spinner).
javascript myPromise.then(...).catch(...) .finally(() => { console.log('Promise operation finished.'); });