Understanding JavaScript Promises
JavaScript Promises are a powerful feature for handling asynchronous operations. Let’s dive into how they work and when to use them.
What is a Promise?
A Promise is an object representing the eventual completion or failure of an asynchronous operation.
Basic Promise Syntax
const myPromise = new Promise((resolve, reject) => {
// Asynchronous operation
if (/* success */) {
resolve(value);
} else {
reject(error);
}
});
Using Promises
You can use the then() and catch() methods to handle promise results:
myPromise
.then(result => {
console.log('Success:', result);
})
.catch(error => {
console.error('Error:', error);
});
Async/Await
Modern JavaScript provides async/await syntax for working with promises:
async function fetchData() {
try {
const response = await fetch('https://api.example.com/data');
const data = await response.json();
return data;
} catch (error) {
console.error('Error fetching data:', error);
}
}
Promises make asynchronous code cleaner and easier to reason about!