Skip to content
Related Articles
Open in App
Not now

Related Articles

JavaScript Promise Complete Reference

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 10 Jan, 2023
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:

JavaScript Promise Static Methods:

Static methods

Description

all()Handle all the asynchronous operations), that take an array of promises(an iterable) as input.
allSettled()Get a promise when all inputs are settled that is either fulfilled or rejected.
race()Returns a single Promise after receiving an iterable of promises as input.
resolve()Returns a Promise object that is resolved with a given value.

JavaScript PromiseInstance Methods:

Instance method

Description

then()Deal with asynchronous tasks such as an API call.
My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!