Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Promise Reference

JavaScript Promise Reference

JavaScript Promise is used to handle asynchronous operations JavaScript. They are easy to manage when dealing with multiple asynchronous operations where callbacks can create callback hell leading to unmanageable code. Promises are the ideal choice for handling asynchronous operations in the simplest manner. They can handle multiple asynchronous operations easily and provide better error handling than callbacks and events.

Syntax

Promise.function();

Example:

Javascript




// Illustration of Promise.allSettled()
// Method in Javascript with Example
  
const p1 = Promise.resolve(50);
const p2 = new Promise((resolve, reject) =>
setTimeout(reject, 100, 'geek'));
const prm = [p1, p2];
  
Promise.allSettled(prm)
.then((results) => results.forEach((result) =>
console.log(result.status,result.value)));


Output:

"fulfilled"
50
"rejected" 
undefined

The complete list of JavaScript Promise is listed below:

JavaScript Promise Constructor: In JavaScript, a constructor gets called when an object is created using the new keyword.

Constructor Description Example
Promise Handle asynchronous operations in JavaScript.
Try

JavaScript Promise Properties: A JavaScript property is a member of an object that associates a key with a value.

  • Instance Property: An instance property is a property that has a new copy for every new instance of the class.
Instance Properties Description Example
constructor Return the promise constructor function for the object.
Try

JavaScript Promise Methods: JavaScript methods are actions that can be performed on objects.

  • Static Method: If the method is called using the array class itself then it is called a static method.
Static Methods Description Example
all() Handle all the asynchronous operations), that take an array of promises(an iterable) as input.
Try
allSettled() Get a promise when all inputs are settled that is either fulfilled or rejected.
Try
any() Return a single promise that fulfills with the value from that promise.
Try
race() Returns a single Promise after receiving an iterable of promises as input.
Try
reject() Returns a Promise object that is rejected with a given reason.
Try
resolve() Returns a Promise object that is resolved with a given value.
Try
  • Instance Method: If the method is called on an instance of a promise then it is called an instance method.
Instance Methods Description Example
catch() The return value of the callback if it is called.
Try
then() Deal with asynchronous tasks such as API calls.
Try
finally() Return a Promise when a Promise is settled, that is, it is either fulfilled or rejected.
Try
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments