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

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)