惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

I
InfoQ
C
CERT Recently Published Vulnerability Notes
The Last Watchdog
The Last Watchdog
P
Proofpoint News Feed
D
Darknet – Hacking Tools, Hacker News & Cyber Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
GbyAI
GbyAI
T
Tenable Blog
博客园 - 三生石上(FineUI控件)
P
Privacy & Cybersecurity Law Blog
Simon Willison's Weblog
Simon Willison's Weblog
Jina AI
Jina AI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Tor Project blog
博客园_首页
F
Fortinet All Blogs
博客园 - Franky
Latest news
Latest news
Last Week in AI
Last Week in AI
T
Threat Research - Cisco Blogs
Scott Helme
Scott Helme
L
LINUX DO - 热门话题
U
Unit 42
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Hugging Face - Blog
Hugging Face - Blog
D
Docker
Project Zero
Project Zero
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
MongoDB | Blog
MongoDB | Blog
F
Full Disclosure
D
DataBreaches.Net
Google DeepMind News
Google DeepMind News
Cisco Talos Blog
Cisco Talos Blog
Y
Y Combinator Blog
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
H
Help Net Security
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
月光博客
月光博客
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Blog — PlanetScale
Blog — PlanetScale
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
Schneier on Security
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed
PCI Perspectives
PCI Perspectives
Cloudbric
Cloudbric
V
Visual Studio Blog
Recorded Future
Recorded Future
人人都是产品经理
人人都是产品经理

Lynx

Lynx | 小熊写字的地方 Lynx | 小熊写字的地方 Lynx | 小熊写字的地方 Lynx | 小熊写字的地方 Lynx | 小熊写字的地方 Lynx | 小熊写字的地方 Lynx | 小熊写字的地方 Lynx | 小熊写字的地方 Lynx | 小熊写字的地方 Lynx | 小熊写字的地方 Lynx | 小熊写字的地方 Lynx | 小熊写字的地方 Lynx | 小熊写字的地方 Lynx | 小熊写字的地方 Lynx | 小熊写字的地方 Lynx | 小熊写字的地方 Lynx | 小熊写字的地方 Lynx | 小熊写字的地方 Lynx | 小熊写字的地方
Lynx | 小熊写字的地方
2018-07-09 · via Lynx

Jordan Harband 提出了 Promise.prototype.finally 这一章节的提案。

如何工作?

.finally() 这样用:

promise
.then(result => {···})
.catch(error => {···})
.finally(() => {···});

finally 的回调总是会被执行。作为比较:

  • then 的回调只有当 promise 为 fulfilled 时才会被执行。
  • catch 的回调只有当 promise 为 rejected,或者 then 的回调抛出一个异常,或者返回一个 rejected Promise 时,才会被执行。
    换句话说,下面的代码段:
    promise
    .finally(() => {
    «statements»
    });

等价于:

promise
.then(
result => {
«statements»
return result;
},
error => {
«statements»
throw error;
}
);

使用案例

最常见的使用案例类似于同步的 finally 分句:处理完某个资源后做些清理工作。不管是一切正常,还是出现了错误,这样的工作都是有必要的。
举个例子:

let connection;
db.open()
.then(conn => {
connection = conn;
return connection.select({ name: 'Jane' });
})
.then(result => {
// Process result
// Use `connection` to make more queries
})
···
.catch(error => {
// handle errors
})
.finally(() => {
connection.close();
});

.finally() 类似于同步代码中的 finally {}

同步代码里,try 语句分为三部分:try 分句,catch 分句和 finally 分句。
对比 Promise:

  • try 分句相当于调用一个基于 Promise 的函数或者 .then() 方法
  • catch 分句相当于 Promise 的 .catch() 方法
  • finally 分句相当于提案在 Promise 新引入的 .finally() 方法

然而,finally {} 可以 return 和 throw ,而在.finally() 回调里只能 throw, return 不起任何作用。这是因为这个方法不能区分显式返回和正常结束的回调。

可用性

深入阅读


原文:http://exploringjs.com/es2018-es2019/ch_promise-prototype-finally.html