The finally
method is invoked regardless of whether the promise is fulfilled or rejected. It takes a callback function as a parameter. It's typically used to perform cleanup operations or to execute code that should run regardless of the promise's outcome.
Let's take the same example of the rocketLaunch
promise from the previous lesson and see how to use the finally
method to remove the duplicate code from the then
and catch
methods.
To try out the finally
method, you can set the value of launchSuccess
variable to both true
and false
and observe the output. In both cases, the output will include the message Rocket launch process completed
from the finally
block.