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

推荐订阅源

C
CXSECURITY Database RSS Feed - CXSecurity.com
GbyAI
GbyAI
aimingoo的专栏
aimingoo的专栏
Recent Announcements
Recent Announcements
Vercel News
Vercel News
The GitHub Blog
The GitHub Blog
A
About on SuperTechFans
G
Google Developers Blog
MyScale Blog
MyScale Blog
N
Netflix TechBlog - Medium
博客园 - 叶小钗
Know Your Adversary
Know Your Adversary
V
Vulnerabilities – Threatpost
D
DataBreaches.Net
P
Palo Alto Networks Blog
C
Cisco Blogs
H
Hackread – Cybersecurity News, Data Breaches, AI and More
NISL@THU
NISL@THU
Forbes - Security
Forbes - Security
Microsoft Security Blog
Microsoft Security Blog
T
The Exploit Database - CXSecurity.com
阮一峰的网络日志
阮一峰的网络日志
腾讯CDC
Schneier on Security
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
N
News and Events Feed by Topic
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Engineering at Meta
Engineering at Meta
Last Week in AI
Last Week in AI
AWS News Blog
AWS News Blog
Security Latest
Security Latest
H
Heimdal Security Blog
小众软件
小众软件
Cyberwarzone
Cyberwarzone
The Hacker News
The Hacker News
P
Privacy International News Feed
Stack Overflow Blog
Stack Overflow Blog
The Cloudflare Blog
Scott Helme
Scott Helme
博客园 - 【当耐特】
Latest news
Latest news
Microsoft Azure Blog
Microsoft Azure Blog
Y
Y Combinator Blog
Jina AI
Jina AI
Spread Privacy
Spread Privacy
量子位
博客园_首页
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Blog — PlanetScale
Blog — PlanetScale
雷峰网
雷峰网

开飞机的老张

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