Ari*_*sry 158 javascript promise bluebird
Bluebird提供了一种"终极"方法,可以在您的保证链中发生任何事情.我发现它非常方便用于清洁目的(比如解锁资源,隐藏装载机......)
在ES6原生承诺中是否有相同的东西?
以下是Finally方法的文档参考:
http://bluebirdjs.com/docs/api/finally.html
谢谢
Mig*_*ota 276
截至2018年2月7日
Chrome 63 +,Firefox 58+和Opera 50+支持Promise.finally.
在Node.js 8.1.4+(V8 5.8+)中,该功能在标志后面可用--harmony-promise-finally.
所述Promise.prototype.finally ECMAScript的提案是目前在阶段3的TC39过程.
同时在所有浏览器中都有承诺.最终功能; 您可以then()在catch()始终调用该回调之后添加其他内容.
例:
myES6Promise.then(() => console.log('Resolved'))
.catch(() => console.log('Failed'))
.then(() => console.log('Always run this'));
Run Code Online (Sandbox Code Playgroud)
JSFiddle演示:https://jsfiddle.net/9frfjcsg/
或者您可以扩展原型以包含finally()方法(不推荐):
Promise.prototype.finally = function(cb) {
const res = () => this;
const fin = () => Promise.resolve(cb()).then(res);
return this.then(fin, fin);
};
Run Code Online (Sandbox Code Playgroud)
JSFiddle演示:https://jsfiddle.net/c67a6ss0/1/
还有Promise.prototype.finally shim库.
| 归档时间: |
|
| 查看次数: |
43446 次 |
| 最近记录: |