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

推荐订阅源

P
Privacy International News Feed
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Google DeepMind News
Google DeepMind News
Hacker News - Newest:
Hacker News - Newest: "LLM"
Google Online Security Blog
Google Online Security Blog
N
News and Events Feed by Topic
TaoSecurity Blog
TaoSecurity Blog
云风的 BLOG
云风的 BLOG
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Recent Announcements
Recent Announcements
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
L
LangChain Blog
The GitHub Blog
The GitHub Blog
aimingoo的专栏
aimingoo的专栏
Scott Helme
Scott Helme
腾讯CDC
T
Threat Research - Cisco Blogs
P
Palo Alto Networks Blog
Cloudbric
Cloudbric
K
Kaspersky official blog
B
Blog RSS Feed
博客园_首页
MongoDB | Blog
MongoDB | Blog
Stack Overflow Blog
Stack Overflow Blog
G
Google Developers Blog
Jina AI
Jina AI
Hugging Face - Blog
Hugging Face - Blog
Vercel News
Vercel News
The Last Watchdog
The Last Watchdog
S
Schneier on Security
T
Tor Project blog
雷峰网
雷峰网
博客园 - 司徒正美
H
Hacker News: Front Page
I
Intezer
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Martin Fowler
Martin Fowler
Y
Y Combinator Blog
T
The Blog of Author Tim Ferriss
Help Net Security
Help Net Security
WordPress大学
WordPress大学
T
The Exploit Database - CXSecurity.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
量子位
V
Vulnerabilities – Threatpost
Forbes - Security
Forbes - Security
Google DeepMind News
Google DeepMind News
T
Threatpost

开飞机的老张

AGENTS.md Openspec 使用心得 从树莓派内网穿透到 Cloudflare Pages Openclaw和博客 郊眠寺 采石 译:我为什么使用Map(和WeakMap)处理DOM节点 介绍JavaScript中Symbol 原生JavaScript获取URL参数 字符串首字母大写 用forEach()遍历对象 JavaScript禁用按钮 JavaScript的FormData JavaScript的Blob 用FileReader读取本地文件 JavaScript中Promise的reject JavaScript的立即调用函数表达式(IIFE) JavaScript的Promise链 用interact.js实现拖拽、缩放、吸附
JavaScript的Thenable
kaifeiji.cc · 2023-07-25 · via 开飞机的老张

原文:Thenables in JavaScript

Thenable是一种行为类似于Promise的对象,主要用于链式操作和async/await,但它并不是Promise。

JavaScript中,Thenable是一种包含then()方法的对象。所有Promise都是Thenable,但并非所有Thenable都是Promise。

许多Promise的模式,比如链式操作async/await,可以用Thenable实现。例如,用Thenable实现一个Promise链:

1
2
3
4
5
6
7
8
9
10
11
12
13
14



const thenable = {
then: function(onFulfilled) {
setTimeout(() => onFulfilled(42), 10);
}
};

Promise.resolve().
then(() => thenable).
then(v => {
v;
});

也可以在await中使用Thenable:

1
2
3
4
5
6
7
8
9
10
11
// Thenable是包含`then()`方法的对象。
// 以下Thenable可以像Promise一样,
// 在10ms后fulfilled,返回42
const thenable = {
then: function(onFulfilled) {
setTimeout(() => onFulfilled(42), 10);
}
};

const v = await thenable;
v; // 42

许多库实现了Thenable来支持async/await。例如,Mongoose的查询就是Thenable,而exec()函数返回的是Promise。Superagent是一个流行的HTTP客户端,也是用了Thenable。Mongoose的查询和Superagent的请求都不是Promise。

其他一些库直接使用Promise。例如,axios的请求就是Promiseinstanceof Promise会返回true。

可以把任意Thenable用Promise.resolve()转换成一个Promise:

1
2
3
4
5
6
7
8
9
10
11
12
13
14



const thenable = {
then: function(onFulfilled) {
setTimeout(() => onFulfilled(42), 10);
}
};

const p = Promise.resolve(thenable);
p instanceof Promise;

const v = await p;
v;

async/await是JavaScript中并发的未来趋势。“Mastering Async/Await”教你如何在几个小时内,用async/await构建前后端APP,来看看吧!