What is the difference between then and finally in a promise?

后端 未结 3 1461
故里飘歌
故里飘歌 2021-01-01 19:14

I see the docs for Bluebird\'s finally but I still don\'t quite understand the difference vs. then.

To be clear: I know exactly why then ge

3条回答
  •  渐次进展
    2021-01-01 19:39

    First difference: Sometimes you don't want to catch errors at the place they arise, but in the code that uses this function, so you don't catch them. In that case you can't substitute then() and finally().

    Sometimes you have to clean something up whether there was an error or not (nulling references, clearing timeouts ... stuff like that). That's where you use finally().

    Second difference: The function you pass to catch() could also throw, then you would have a rejected Promise and the following then() would not be called.

    (so a finally before a catch will still execute on an error, didn't know that)

    Yeah, that's the point of finally(). It will be executed under any circumstance without changing the resolved value.

    You might want to read/google a bit about try {} finally {}, without catch.

提交回复
热议问题