Skip to content
Related Articles

Related Articles

Improve Article

JavaScript | Promise.allSettled() Method

  • Last Updated : 02 Jun, 2020

The Promise in JavaScript can be in three states pending, fulfilled or rejected. The Promise.allSettled() method in Javascript is used to get a promise when all inputs are settled that is either fulfilled or rejected.

Syntax:

Promise.allSettled(iterable);

Parameters: This method accept a single parameter iterable which takes an array of promises or a normal array which contains some objects.

Return Value: This method returns the following values:

  • If passed argument is empty, it returns a Promise that already resolved.
  • For all other cases, it returns a pending Promise.

Example 1:



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

Example 2:

Javascript




<script>
    // Simple promise that resolves 
    // After a given time 
    const tOut = (t) => { 
      return new Promise((resolve, reject) => { 
        setTimeout(() => { 
          resolve(`Completed in ${t}`) 
        }, t) 
      }) 
    
    
    // Resolving a normal promise 
    tOut(1000).then(result => console.log(result))  
    // Completed in 1000 
    
    // Promise.allSettled 
    Promise.allSettled([tOut(1000), tOut(2000)]).then(result => 
    console.log(result)) 
</script>

Output:

"Completed in 1000"
Array [Object { status: "fulfilled", value: "Completed in 1000" }, 
Object { status: "fulfilled", value: "Completed in 2000" }]

Supported Browsers: The browsers supported by JavaScript Promise.allSettled() method are listed below:

  • Google Chrome
  • Microsoft Edge
  • Mozilla Firefox
  • Apple Safari
  • Opera



My Personal Notes arrow_drop_up
Recommended Articles
Page :