In JavaScript, a Promise is an object that represents the eventual completion (or failure) of an asynchronous operation and its resulting value. Promises provide a more structured and manageable way to handle asynchronous code compared to traditional callbacks.
A Promise can be in one of three states:
Pending: The initial state. The Promise is still being processed, and the final value is not yet available.
Fulfilled: The asynchronous operation has completed successfully, and the Promise has a resolved value.
Rejected: The asynchronous operation encountered an error or was unsuccessful, and the Promise has a reason for rejection.
Here's an example of using a Promise to fetch data asynchronously from an API:
const getData = new Promise((resolve, reject) => {
// Asynchronous operation, such as making an API request
const apiUrl = 'https://api.example.com/data';
// Simulating a successful response
setTimeout(() => {
const data = { name: 'John', age: 30 };
resolve(data); // Resolve the Promise with the retrieved data
}, 2000);
// Simulating an error response
// setTimeout(() => {
// reject(new Error('Failed to fetch data')); // Reject the Promise with an error
// }, 2000);
});
// Using the Promise
getData
.then((data) => {
// Handle the resolved value
console.log(data);
})
.catch((error) => {
// Handle the rejection reason
console.error(error);
});
In the above example, the getData Promise is created, representing an asynchronous operation. Inside the Promise constructor, you perform the necessary asynchronous task, such as making an API request. If the operation is successful, you call resolve() with the retrieved data, and if it fails, you call
reject() with an error.
To handle the Promise, you chain .then() to specify what to do with the resolved value when the Promise is fulfilled. You can also chain
.catch() to handle any errors or rejections that occur during the Promise's lifecycle.
Promises can also be combined using methods like Promise.all() or
Promise.race() to handle multiple asynchronous operations concurrently or to handle the first resolved/rejected Promise, respectively.
Promises simplify working with asynchronous code by providing a more structured and readable approach. They allow you to handle success and error cases separately, avoiding callback hell and improving code maintainability.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In JavaScript, a Promise is an object that represents the eventual completion (or failure) of an asynchronous operation and its resulting value. Promises provide a more structured and manageable way to handle asynchronous code compared to traditional callbacks.
A Promise can be in one of three states:
Here's an example of using a Promise to fetch data asynchronously from an API:
In the above example, the getData Promise is created, representing an asynchronous operation. Inside the Promise constructor, you perform the necessary asynchronous task, such as making an API request. If the operation is successful, you call resolve() with the retrieved data, and if it fails, you call reject() with an error.
To handle the Promise, you chain .then() to specify what to do with the resolved value when the Promise is fulfilled. You can also chain .catch() to handle any errors or rejections that occur during the Promise's lifecycle.
Promises can also be combined using methods like Promise.all() or Promise.race() to handle multiple asynchronous operations concurrently or to handle the first resolved/rejected Promise, respectively.
Promises simplify working with asynchronous code by providing a more structured and readable approach. They allow you to handle success and error cases separately, avoiding callback hell and improving code maintainability.