Promises represent a value that may not be available yet, providing a cleaner alternative to callbacks for handling asynchronous operations. Unlike callbacks, Promises can be chained and help avoid “callback hell.” Example of Promise chaining:
const fetchData = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
const data = 'Data fetched!';
resolve(data);
}, 1000);
});
};
fetchData()
.then(data => console.log(data))
.catch(error => console.error(error));