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

推荐订阅源

AI
AI
Scott Helme
Scott Helme
W
WeLiveSecurity
N
News | PayPal Newsroom
G
GRAHAM CLULEY
SecWiki News
SecWiki News
V2EX - 技术
V2EX - 技术
Security Latest
Security Latest
H
Heimdal Security Blog
L
LINUX DO - 最新话题
Application and Cybersecurity Blog
Application and Cybersecurity Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
P
Palo Alto Networks Blog
Simon Willison's Weblog
Simon Willison's Weblog
PCI Perspectives
PCI Perspectives
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
Spread Privacy
Spread Privacy
T
Troy Hunt's Blog
V
Vulnerabilities – Threatpost
罗磊的独立博客
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
T
The Blog of Author Tim Ferriss
阮一峰的网络日志
阮一峰的网络日志
Martin Fowler
Martin Fowler
The Hacker News
The Hacker News
C
Cisco Blogs
T
Tor Project blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The Register - Security
The Register - Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
P
Privacy & Cybersecurity Law Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
S
Security Affairs
T
Tenable Blog
V
Visual Studio Blog
C
Check Point Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
美团技术团队
月光博客
月光博客
J
Java Code Geeks
量子位
Vercel News
Vercel News
I
Intezer
博客园 - 聂微东
Know Your Adversary
Know Your Adversary
aimingoo的专栏
aimingoo的专栏

开飞机的老张

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,来看看吧!