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

推荐订阅源

雷峰网
雷峰网
宝玉的分享
宝玉的分享
I
InfoQ
P
Privacy International News Feed
V
V2EX
IT之家
IT之家
S
SegmentFault 最新的问题
D
Darknet – Hacking Tools, Hacker News & Cyber Security
V2EX - 技术
V2EX - 技术
C
CERT Recently Published Vulnerability Notes
C
Check Point Blog
The Register - Security
The Register - Security
爱范儿
爱范儿
博客园 - 三生石上(FineUI控件)
AWS News Blog
AWS News Blog
M
MIT News - Artificial intelligence
C
Cyber Attacks, Cyber Crime and Cyber Security
F
Fortinet All Blogs
B
Blog
N
Netflix TechBlog - Medium
B
Blog RSS Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Last Week in AI
Last Week in AI
T
Threatpost
Forbes - Security
Forbes - Security
U
Unit 42
A
Arctic Wolf
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
P
Palo Alto Networks Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Recorded Future
Recorded Future
L
Lohrmann on Cybersecurity
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
月光博客
月光博客
Spread Privacy
Spread Privacy
MongoDB | Blog
MongoDB | Blog
Jina AI
Jina AI
I
Intezer
V
Visual Studio Blog
阮一峰的网络日志
阮一峰的网络日志
The Hacker News
The Hacker News
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
L
LangChain Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园_首页
MyScale Blog
MyScale Blog
腾讯CDC
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
量子位

开飞机的老张

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