es6 Promise.done(),Promise.finally()

Promise.prototype.done = function (onFulfilled, onRejected) {
    this.then(onFulfilled, onRejected)
    .catch(function (reason) {
        // 抛出一个全局错误
        setTimeout(() => { throw reason }, 0);
    });
};

finally方法用于指定不管 Promise 对象最后状态如何,都会执行的操作。

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 })
    );
};

猜你喜欢

转载自blog.csdn.net/qq_31687021/article/details/89536620