实现 Promise finally

Promise 实现 finally 方法

废话不多说,比较简单,直接上代码吧

1
2
3
4
5
6
7
Promise.prototype.finally = function (callback) {
let P = this.constructor;
return this.then(
value => P.resolve(callback()).then(() => value),
reason => P.resolve(callback()).then(() => { throw reason })
);
};