const fetchData = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
reject('Error fetching data!');
}, 1000);
});
};
fetchData()
.then(response => {
console.log(response);
})
.catch(error => {
console.error(error); // Output: Error fetching data!
});
While Promises improved the readability of asynchronous code, async
and await
take it a step further by allowing developers to write asynchronous code that looks synchronous.