Skilled in SEO, content writing, and digital marketing. Completed several years of working in many organizations including multinational companies.
I love to learn new things in life that keep me motivated.
To handle the result of a resolved Promise in JavaScript, you can use the
.then() method. The .then() method allows you to specify a callback function that will be executed when the Promise is resolved successfully (i.e., in the fulfilled state). Here's how you can handle the result of a resolved Promise:
const myPromise = new Promise((resolve, reject) => {
// Simulate a successful asynchronous operation
setTimeout(() => {
resolve("Operation succeeded!"); // Resolve the promise with a result
}, 2000);
});
myPromise.then((result) => {
// This function will be called when the Promise is resolved successfully
console.log("Success:", result);
});
In this example, when myPromise is resolved (after a 2-second delay), the
.then() method's callback function is called, and you can access the resolved value (in this case, the string "Operation succeeded!") as the
result parameter within the callback.
You can also chain multiple .then() methods together for handling multiple asynchronous operations in sequence or for creating more complex Promise chains.
Here's an example with chaining:
const promise1 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("First operation succeeded!");
}, 2000);
});
const promise2 = promise1.then((result) => {
console.log("First Promise Result:", result);
// You can return a new Promise or a value here
return "Second operation succeeded!";
});
promise2.then((result) => {
console.log("Second Promise Result:", result);
});
In this chained example, the second .then() handler is called with the result of the first Promise, and you can continue processing or returning new Promises as needed. This chaining mechanism is one of the strengths of Promises in managing complex asynchronous flows.
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.
To handle the result of a resolved Promise in JavaScript, you can use the .then() method. The .then() method allows you to specify a callback function that will be executed when the Promise is resolved successfully (i.e., in the fulfilled state). Here's how you can handle the result of a resolved Promise:
In this example, when myPromise is resolved (after a 2-second delay), the .then() method's callback function is called, and you can access the resolved value (in this case, the string "Operation succeeded!") as the result parameter within the callback.
You can also chain multiple .then() methods together for handling multiple asynchronous operations in sequence or for creating more complex Promise chains.
Here's an example with chaining:
In this chained example, the second .then() handler is called with the result of the first Promise, and you can continue processing or returning new Promises as needed. This chaining mechanism is one of the strengths of Promises in managing complex asynchronous flows.