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

推荐订阅源

Project Zero
Project Zero
F
Fortinet All Blogs
博客园 - 叶小钗
阮一峰的网络日志
阮一峰的网络日志
月光博客
月光博客
爱范儿
爱范儿
博客园_首页
S
Security @ Cisco Blogs
H
Heimdal Security Blog
Cyberwarzone
Cyberwarzone
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Tailwind CSS Blog
T
Threatpost
博客园 - 司徒正美
The GitHub Blog
The GitHub Blog
WordPress大学
WordPress大学
S
Security Affairs
AWS News Blog
AWS News Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cisco Talos Blog
Cisco Talos Blog
博客园 - 聂微东
P
Privacy & Cybersecurity Law Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
NISL@THU
NISL@THU
G
GRAHAM CLULEY
人人都是产品经理
人人都是产品经理
L
LangChain Blog
The Register - Security
The Register - Security
L
LINUX DO - 最新话题
博客园 - 【当耐特】
P
Proofpoint News Feed
Schneier on Security
Schneier on Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
P
Proofpoint News Feed
IT之家
IT之家
云风的 BLOG
云风的 BLOG
量子位
D
Darknet – Hacking Tools, Hacker News & Cyber Security
PCI Perspectives
PCI Perspectives
腾讯CDC
大猫的无限游戏
大猫的无限游戏
美团技术团队
N
News | PayPal Newsroom
Recent Commits to openclaw:main
Recent Commits to openclaw:main
D
Docker
A
Arctic Wolf
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Cloudbric
Cloudbric

开飞机的老张

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