How does the following scheme simulate the finally function of promise?

Source address

found an interesting piece of code in it

Promise.prototype["finally"] = function(callback) {
    var constructor = this.constructor
    return this.then(
        function(value){
            return constructor.resolve(callback()).then(function(){
                return value;
            })
        },
        function(reason){
            return constructor.resolve(callback()).then(function(){
                return constructor.reject(reason)
            })
        }
    )
}

this code is how to simulate the function of finally, solve, thank you!

Mar.05,2021
Menu