Skip to content
Related Articles

Related Articles

JavaScript Promise Complete Reference

Improve Article
Save Article
  • Last Updated : 23 Nov, 2022
Improve Article
Save Article

JavaScript Promises are 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




<script>
    // 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)));
</script>

Output:

"fulfilled"
50
"rejected" 
undefined

The complete list of JavaScript Promise are listed below:

Static Methods:

JavaScript Promise Static methods

Description 

JavaScript Promise.all() MethodThe Promise.all() method is actually a method of Promise object (which is also an object under JavaScript used to handle all the asynchronous operations), that takes an array of promises(an iterable) as an input.
JavaScript Promise.allSettled() MethodThe Promise.allSettled() is used to get a promise when all inputs are settled that is either fulfilled or rejected.
JavaScript Promise.race() MethodThe Promise.race() method returns a promise that fulfills or rejects as soon as one of the promises in an iterable fulfills or rejects, with the value or reason from that promise.
JavaScript promise resolve() MethodPromise.resolve() method in JS returns a Promise object that is resolved with a given value. 

Instance Methods:

JavaScript Promise Instance method

Description 

JavaScript promise then() MethodThe then() method in JavaScript has been defined in the Promise API and is used to deal with asynchronous tasks such as an API call. 
My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!