01 / Foundation
Understand the concept
A Promise represents the eventual completion or failure of an asynchronous operation.
Promises provide a cleaner and more maintainable way to handle asynchronous code compared to nested callbacks.
02 / Implementation
Creating a Promise
promise.js
const fetchData = new Promise((resolve, reject) => {
const success = true;
if (success) {
resolve("Data received");
} else {
reject("Something went wrong");
}
});
fetchData
.then((data) => console.log(data))
.catch((error) => console.log(error));04 / Interview