2

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.stack.imgur.com/HeQV8.png

  • post the code snippet here, not the image – Sachin Ananthakumar Sep 30 '21 at 22:08
  • 1
    **DO NOT post images of code, data, error messages, etc.** - copy or type the text into the question. Please reserve the use of images for diagrams or demonstrating rendering bugs, things that are impossible to describe accurately via text. For more information please see the Meta FAQ entry [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question) – VLAZ Sep 30 '21 at 22:10
  • Paste that into the question, not a comment. – Barmar Sep 30 '21 at 22:12
  • You can use a [Stack Snippet](https://meta.stackoverflow.com/questions/358992/ive-been-told-to-create-a-runnable-example-with-stack-snippets-how-do-i-do) to make it executable. – Barmar Sep 30 '21 at 22:12

2 Answers2

5

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'))
Barmar
  • 741,623
  • 53
  • 500
  • 612
Michael G
  • 458
  • 2
  • 9
4

Sure, finally is a chainable promise method just like catch (with the only difference that its callback does not take a parameter). You can use it as many times as you want, on any promise.

Promise.all([
    p1.finally(() => { console.log("p1 completed"); }),
    p2.finally(() => { console.log("p2 completed"); }),
]).finally(() => {
    console.log("all() completed");
})
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • thanks, yes that was my question, but the .finally() console.logs are executed first and then the .then() console.logs. Is the order of execution correct that way, I thought the .then() was executed first and then the .finally(), see.... // p1 completed // p2 completed // p1 // p2 // all() completed –  Sep 30 '21 at 22:38
  • @DanielTinajeroDíaz I'm not sure what code you are running, but promise chains execute in the order of the chaining. Notice that each of the methods returns a new promise that resolves after the callback has run. The only difference is that for `finally`, the callback runs on both fulfillments and rejections. – Bergi Sep 30 '21 at 22:42
  • In a promise, which is executed first, the .finally() or the .then() handlers? I'm sorry, I am almost beginner in promises –  Sep 30 '21 at 22:49
  • @DanielTinajeroDíaz Still you haven't posted code that calls both `.then()` and `.finally()`, but maybe have a look at [this](https://stackoverflow.com/q/27923253/1048572) and [that](https://stackoverflow.com/q/41926937/1048572) - what's said there for `then` equally holds for `finally`. – Bergi Sep 30 '21 at 22:57
  • ready, I've edit the code in the question, what would be the order of execution?, finally() first or .then()? –  Sep 30 '21 at 23:11
  • @DanielTinajeroDíaz In `b = a.then(…), c = b.finally(…)` (or `c = a.then(…).finally(…)` for short), the `b` promise is resolved with the result of the callback on `a` (which runs when `a` is fulfilled), and `c` promise is resolved when the result is the callback on `b` is finished (which runs when `b` is settled). There's only one order in which this can possibly happen. – Bergi Sep 30 '21 at 23:23