什么是相当于蓝鸟的承诺。最后在本地的ES6承诺?(复制)

时间:2022-08-23 11:58:50

This question already has an answer here:

这个问题已经有了答案:

Bluebird offer a "finally" method that is being called whatever happens in your promise chain. I find it very handy for cleaning purposes (like unlocking a resource, hiding a loader, ...)

蓝鸟提供了一种“最终”的方法,它被称为你承诺链中发生的任何事情。我发现它非常适合用于清理目的(比如解锁资源、隐藏加载程序…)

Is there an equivalent in ES6 native promises?

ES6原生承诺中是否有对应的?

Here is the documentation reference for the Finally method:

以下是最终方法的文档参考:

http://bluebirdjs.com/docs/api/finally.html

http://bluebirdjs.com/docs/api/finally.html

Thanks

谢谢

1 个解决方案

#1


235  

As of February 7, 2018

从2018年2月7日开始

Chrome 63+, Firefox 58+, and Opera 50+ support Promise.finally.

Chrome 63+, Firefox 58+和Opera 50+支持承诺。

In Node.js 8.1.4+ (V8 5.8+), the feature is available behind the flag --harmony-promise-finally.

在节点。js 8.1.4+ (V8 5.8+),这个特性可以在标志后面找到——harmony-promise-finally。

The Promise.prototype.finally ECMAScript Proposal is currently in stage 3 of the TC39 process.

Promise.prototype。最后,ECMAScript提案目前处于TC39流程的第三阶段。

In the meantime to have promise.finally functionality in all browsers; you can add an additional then() after the catch() to always invoke that callback.

与此同时,要有承诺。最后是所有浏览器的功能;您可以在catch()之后添加额外的then(),以始终调用该回调。

Example:

例子:

myES6Promise.then(() => console.log('Resolved'))
            .catch(() => console.log('Failed'))
            .then(() => console.log('Always run this'));

JSFiddle Demo: https://jsfiddle.net/moogs/9frfjcsg/

JSFiddle演示:https://jsfiddle.net/moogs/9frfjcsg/

Or you can extend the prototype to include a finally() method (not recommended):

或者扩展原型,包括finally()方法(不推荐):

Promise.prototype.finally = function(cb) {
    const res = () => this;
    const fin = () => Promise.resolve(cb()).then(res);
    return this.then(fin, fin);
};

JSFiddle Demo: https://jsfiddle.net/moogs/c67a6ss0/1/

JSFiddle演示:https://jsfiddle.net/moogs/c67a6ss0/1/

There's also the Promise.prototype.finally shim library.

还有Promise.prototype。最后垫片图书馆。

#1


235  

As of February 7, 2018

从2018年2月7日开始

Chrome 63+, Firefox 58+, and Opera 50+ support Promise.finally.

Chrome 63+, Firefox 58+和Opera 50+支持承诺。

In Node.js 8.1.4+ (V8 5.8+), the feature is available behind the flag --harmony-promise-finally.

在节点。js 8.1.4+ (V8 5.8+),这个特性可以在标志后面找到——harmony-promise-finally。

The Promise.prototype.finally ECMAScript Proposal is currently in stage 3 of the TC39 process.

Promise.prototype。最后,ECMAScript提案目前处于TC39流程的第三阶段。

In the meantime to have promise.finally functionality in all browsers; you can add an additional then() after the catch() to always invoke that callback.

与此同时,要有承诺。最后是所有浏览器的功能;您可以在catch()之后添加额外的then(),以始终调用该回调。

Example:

例子:

myES6Promise.then(() => console.log('Resolved'))
            .catch(() => console.log('Failed'))
            .then(() => console.log('Always run this'));

JSFiddle Demo: https://jsfiddle.net/moogs/9frfjcsg/

JSFiddle演示:https://jsfiddle.net/moogs/9frfjcsg/

Or you can extend the prototype to include a finally() method (not recommended):

或者扩展原型,包括finally()方法(不推荐):

Promise.prototype.finally = function(cb) {
    const res = () => this;
    const fin = () => Promise.resolve(cb()).then(res);
    return this.then(fin, fin);
};

JSFiddle Demo: https://jsfiddle.net/moogs/c67a6ss0/1/

JSFiddle演示:https://jsfiddle.net/moogs/c67a6ss0/1/

There's also the Promise.prototype.finally shim library.

还有Promise.prototype。最后垫片图书馆。