JavaScript Promises

Level : Advanced
Mentor: Shailendra Chauhan
Duration : 00:02:00

States of a JavaScript Promise:

A JavaScript Promise can be in one of three states: pending, resolved, or rejected.

Example:

let promise = new Promise((resolve, reject) => {
  // Resolving the promise
  resolve("Success!");
  // Rejecting the promise
  // reject("Error!");
}); 

The .catch() method for handling rejection:

The '.catch()' method is used to handle rejected promises and provide an alternative to the second argument of '.then()' for handling errors.

Example:

promise.catch((error) => {
  console.log("Error:", error);
}); 

JavaScript Promise.all():

'Promise.all()' takes an array of promises and returns a new promise that resolves when all input promises have resolved or rejects if any of them rejects.

Example:

let promise1 = new Promise(/* ... */);
let promise2 = new Promise(/* ... */);
Promise.all([promise1, promise2]).then((results) => {
  console.log("Resolved:", results);
}).catch((error) => {
  console.log("Error:", error);
}); 

Executor function of JavaScript Promise object:

The executor function inside the Promise constructor is where you define the logic for resolving or rejecting the promise.

Example:

let executorFn = (resolve, reject) => {
  if (/* condition */) {
    resolve("Resolved!");
  } else {
    reject("Rejected!");
  }
};
let promise = new Promise(executorFn);

.then() method of a JavaScript Promise object:

The '.then()' method is used to handle the resolved or rejected value of a promise.

Example:

promise.then((resolvedValue) => {
  console.log("Resolved:", resolvedValue);
}).catch((error) => {
  console.log("Error:", error);
});

setTimeout():

'setTimeout()' is an asynchronous function that executes a callback function after a specified delay.

Example:

let showMessage = () => {
  console.log("Delayed message.");
};
setTimeout(showMessage, 1000); 

Avoiding nested Promise and .then():

Chaining '.then()' methods is preferred over nesting for better readability and maintainability.

Example:

promise.then((result) => {
  return doSomething(result);
}).then((newResult) => {
  return doAnotherThing(newResult);
}).catch((error) => {
  console.log("Error:", error);
}); 

Creating a Javascript Promise object:

A Promise object is created using the 'new Promise()' constructor and an executor function.

Example:

let promise = new Promise((resolve, reject) => {
  // Resolve or reject logic
});

The Promise Object:

Promises are used to handle asynchronous operations and their outcomes, allowing non-blocking execution.

Example:

let fetchData = new Promise((resolve, reject) => {
  // Fetch data logic
}); 

Chaining multiple .then() methods:

Chaining '.then()' methods allows sequential processing of resolved values from promises.

Example:

let promise = new Promise((resolve) => {
  resolve("Alan");
});
promise.then((name) => {
  return 'Hello, ${name}!';
}).then((greeting) => {
  console.log(greeting);
}).catch((error) => {
  console.log("Error:", error);
});
Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this