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