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

推荐订阅源

Hacker News: Ask HN
Hacker News: Ask HN
C
Cisco Blogs
The Hacker News
The Hacker News
T
Tor Project blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The GitHub Blog
The GitHub Blog
A
Arctic Wolf
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The Register - Security
The Register - Security
云风的 BLOG
云风的 BLOG
Simon Willison's Weblog
Simon Willison's Weblog
P
Palo Alto Networks Blog
Vercel News
Vercel News
C
CERT Recently Published Vulnerability Notes
I
InfoQ
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
M
MIT News - Artificial intelligence
I
Intezer
aimingoo的专栏
aimingoo的专栏
U
Unit 42
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LINUX DO - 热门话题
Microsoft Security Blog
Microsoft Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyberwarzone
Cyberwarzone
P
Proofpoint News Feed
P
Proofpoint News Feed
B
Blog
T
Threat Research - Cisco Blogs
博客园 - 叶小钗
Recorded Future
Recorded Future
Last Week in AI
Last Week in AI
N
News and Events Feed by Topic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Know Your Adversary
Know Your Adversary
Engineering at Meta
Engineering at Meta
G
Google Developers Blog
PCI Perspectives
PCI Perspectives
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
Application and Cybersecurity Blog
Application and Cybersecurity Blog
MyScale Blog
MyScale Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Schneier on Security
Schneier on Security
N
News | PayPal Newsroom
C
Cybersecurity and Infrastructure Security Agency CISA
H
Help Net Security
博客园 - 聂微东
H
Hackread – Cybersecurity News, Data Breaches, AI and More
G
GRAHAM CLULEY

JavaScript

发现国内很多文章对宏任务和微任务的解释都是有问题的 - V2EX 智能香氛机小程序外包合作招募 - V2EX 分享一道 Node.js 的面试题,考察 JS 相关的最基本的掌握程度,代码完全来自官方文档 你们 js 用过双等号吗 - V2EX 有人大量建立技术文档的.cn 中文站点,并将站内 js 链接指向 cdn.jsdelivr.net.cn - V2EX 发现了两个关于 JavaScript 语言好玩的网站,让人血压升高 js 判空值 最佳实践 - V2EX 简单的图像算法为什么 AI 搞不定呢 Capacitor.JS 如何自动化最后一步流程 JSON.parse 转数字精度丢失问题 有没有前端大佬,帮我看一下这个网站底部的粒子动画怎么做的。 - V2EX 这段前端代码存在并发读取竞态的问题吗? nstr - number → string, but looks good 前端 js 播放 base64 编码的 mp3,放出来杂音很大,请教是哪里出错了? 熟悉工业计算的来,是阀门相关的企业 js 不使用 promise 完成一个 sleep 函数,必须支持 await 调用 - V2EX 小米手机澎湃系统 AutoJS 的悬浮窗在证券软件打开时就消失 - V2EX 关于 js 复制粘贴问题 求助,无法在 wixsite.com 上使用 document.write threejs 官方文档看着好难受。 - V2EX 有没有 AI 能一步一步教会伸手党如何使用业务系统? 也聊下自己最近用 copilot 写程序的心得,还要讨教下方案 javascript 继续 套壳 AI 应用类 manus 如何在移动端 h5 页面实现<Select/>搜索下拉选择框! - V2EX 求前端高频数据绘图库 - V2EX 吐槽一下 Trae 记忆中好像在论坛看到有款专门在 iframe 中做前端的 js 库/框架? - V2EX 求问,小程序的菜单栏怎么和右边的功能键垂直居中? - V2EX 自习室小程序的座位图是如何实现的(前端) - V2EX 谷歌浏览器的 bug - V2EX 为什么中小公司的前端面试官爱问 array 和 object 有多少种方法 - V2EX 有什么办法能实现 uniapp 上的离线人脸识别 本人遇到了非常奇怪的关于“注释”ctrl+/ 快捷键问题,需要大佬们指点一二。 ChatGPT 语音对话技术 求前端大佬解惑, HTML 里的文本怎么做逐行滚动? 快应用问题求助 2024 JS 生态调查结果出炉: React 遥遥领先, Vue 程序员最穷 不顾“万人联名请愿”,甲骨文拒绝放弃 JavaScript 商标所有权 2025 年,我又把前端"老古董"--AMD 给翻了出来 有使用 javascript 编写 windows cmd 批处理程序的方法吗,类似 coffescript 这样转译的东西 js 小白,请教一下为什么网页上的播放器, Chrome 控制台不能用 querySelector 直接获取到呢 自荐一个 js 通信的包 Python 代码转换为 Javascript 运算结果不一致 css 的 justify-content 等效语法? 请教一下使用油猴脚本修改 html 内容后, 样式不生效的问题 async 函数中止/task local 方案后记,V8 上的另一个可行思路 如何在前端统计用户观看视频所消耗的流量?求教啊,大神们! pdfjs 中,通过 findController 可以搜索到对应的文本信息,并且高亮出来。那么, pdfjs 有没有什么 api 可以获取搜索到的信息的位置节点 ?请教 有没有独立的同时可以 pc 移动端用的三级联动城市选择组件啊 - V2EX 字幕滚动有没有更好的方式实现
JavaScript 中的 Promise 跟异步有关系吗?还是我的理解有问题?谁能把 Promise 解释清楚? Promise 的正确用法应该是什么样的? - V2EX
burnbrid · 2025-03-15 · via JavaScript
yazinnnn0

26

yazinnnn0      2025 年 3 月 15 日   ❤️ 2

Promise 借鉴了 Monad 的一些设计思想, 用 CPS 变换优化实现链式组合异步操作(解决异步的回调地狱)
async await 是类似于 Monad Comprehensions 的语法糖实现, 不过运行机制不一样, async await 依赖 javascript 运行时模型

原有的异步调用代码
asyncOp1((result1) => {
asyncOp2(result1, (result2) => {
asyncOp3(result2, (result3) => { /* ... */ });
});
});

Promise 优化之后的代码
asyncOp1()
.then((result1) => asyncOp2(result1))
.then((result2) => asyncOp3(result2))
.then((result3) => { /* ... */ });

使用 Monad Comprehensions 语法糖将中间变量从回调中取出来
result1 = await asyncOp1()
result2 = await asyncOp2(result1)
result3 = await asyncOp3(result2)

题外话, promise 不是标准的 monad, 但是有很多 monad 的特性, 比如统一的异常处理 (async await 之后就可以异步 try catch 了), 不然的话你想想每个回调都传一个 reject 参数来处理异常 (人肉 if err != nil 是吧)

所以说那些 go boy 用 if err != nil 来嘲笑 try catch 就挺逆天的, 学术界用几十年的时间来消除 errcode, 然后这玩意儿在 go boy 嘴里成最优雅的语言了😅

题外话 2, 其他语言的 Monad Comprehensions 语法糖举例
haskell
compute :: Maybe Int
compute = do
x <- Just 3
y <- Just 5
return (x + y) -- 结果为 Just 8

csharp
var query = from x in new[] { 1, 2 }
from y in new[] { 3, 4 }
select x + y; // 结果为 { 4, 5, 5, 6 }

fsharp
let asyncExample = async {
let! x = async { return 3 }
let! y = async { return 5 }
return x + y
}

scala
val compute: Option[Int] = for {
x <- Some(3)
y <- Some(5)
} yield x + y

swift
func compute() async -> Int {
let x = await fetchData()
let y = await processData(x)
return y
}

kotlin
suspend fun compute(): Int {
val x = async { fetchData() }
val y = async { processData(x.await()) }
return y.await()
}

java
没有😅

go
没有😅

rust
没有语法糖, 有个?(操作符)可以模拟类似功能(result option)

基本上后来兴起的工程向的语言都给异步相关的 api 实现了 monad comprehension, 一部分语言(fsharp kotlin 啥的)可以自定义语法

题外话 3
貌似一些 lisp people 极度反感 monad (和 go boy 殊途同归了属于是), clojure 社区里贴大字报明确反对标准库里添加 monad 实现, 吵了好几十条😅