Lesson Overview
In this lesson, we will learn about Promises in JavaScript, how they work, and how to use them to handle asynchronous operations.
Graphical Preview
Graphical representation of Promises will be shown here.
Code Preview
const myPromise = new Promise((resolve, reject) => {
// Simulate an asynchronous operation
setTimeout(() => {
const success = true; // Change to false to simulate an error
if (success) {
resolve("Operation was successful!");
} else {
reject("Operation failed.");
}
}, 2000);
});
myPromise
.then(result => console.log(result))
.catch(error => console.error(error));