Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I have multiple .finally() as well as multiple .catch() in Promise.all()?

var p1 = new Promise((resolve, reject) => {

        resolve('p1');
    });

    var p2 = new Promise((resolve, reject) => {
        resolve('p2');
    });
    Promise.all([
        p1.finally(() => { console.log("p1 completed"); }),
        p2.finally(() => { console.log("p2 completed"); }),
    ]).then(values => {
        console.log(values[0]);
        console.log(values[1]);
    }).finally(() => {
        console.log("all() completed");
  

I think I've only seen examples on the web with a single .finally() at the end [1]: https://i.sstatic.net/HeQV8.png


1 Answers

You may chain as many .finally() calls as you like onto any promise.
(Promise.all() returns a new promise, so this rule applies here as well.)

Run this, and you should see all 3 comments log.

Promise.resolve().
  finally(() => console.log('Finally #1')).
  finally(() => console.log('Finally #2')).
  finally(() => console.log('Finally #3'))
like image 107
Michael G Avatar answered Jan 02 '26 05:01

Michael G



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!