Promises in JavaScript are objects representing the eventual completion or failure of an asynchronous operation. They are used to handle asynchronous operations like API calls, file reading, etc., in a more manageable way. A promise can be in one of three states:
Pending: The initial state, neither fulfilled nor rejected. Fulfilled: The operation completed successfully. Rejected: The operation failed. Here's a basic structure of a promise:
let myPromise = new Promise((resolve, reject) => {
// Asynchronous operation code here
const success = /* condition to check if operation was successful */;
if (success) {
resolve('Operation successful'); // Resolve the promise
} else {
reject('Operation failed'); // Reject the promise
}
});
myPromise.then((message) => {
console.log('Success: ' + message);
}).catch((message) => {
console.log('Failure: ' + message);
});
In this example, myPromise is a new Promise object. The constructor takes a function with two arguments, resolve and reject. These are methods used to determine the outcome of the promise:
resolve(value) is called when the operation succeeds, and it transitions the promise from "pending" to "fulfilled." reject(reason) is called when the operation fails, transitioning the promise to "rejected." After the promise's operation, you can use .then() to handle the case when the promise is fulfilled and .catch() to handle the case when the promise is rejected.
function myAsyncFunction(url) {
return new Promise((resolve, reject) => {
// Simulating an asynchronous API call
setTimeout(() => {
const success = Math.random() > 0.5; // Random success or failure
if (success) {
resolve(`Fetched data from ${url}`);
} else {
reject(`Failed to fetch data from ${url}`);
}
}, 2000); // 2 seconds delay
});
}
myAsyncFunction('https://example.com')
.then(data => console.log(data))
.catch(error => console.error(error));
In this example, myAsyncFunction simulates an API call with a random outcome. After 2 seconds, it either resolves with a success message or rejects with an error message. The .then() handles the successful response, and .catch() deals with any errors.