Comments (12)
感觉博主说的console这块有点问题啊,console是基于process.stdout.write,但是console.log(a),a这个参数是直接传进来的:代码链接。
这这里是对args的处理,再看write:代码链接,在调用process.stdout.write之前已经拼接好string了,所以应该是console.log的args是同步的,只不过在输出的时候调用的process.stdout.write。
而write的异步问题,确实代码里面进行了兼容处理了:代码链接,通过event emitter的once方法对error进行监听,这样就相当于把write丢到了libuv的event loop中了,无论同步异步,只要触发了error,就会打断,而下面的catch,则是怕运行栈溢出,直接hold住uv_defalut_loop了,所以会catch一下,做下溢出判断。
from issue-blog.
但是wirte里面只是调用了stream.write(string, errorhandler);而且try里面只是捕获同步错误,对于你上面说的不论同步还是异步,都会走进catch有点不太理解,而且这个容错处理和stream.write是同步还是异步有什么关系呢
from issue-blog.
异步不会走到catch,我这里说的确实有问题。
你关联一下上下文,stream.write其实是process.stdout.write,而process.stdout官网api文档介绍了是一个socket流,因此会继承于event emitter,所以
stream.once('error', noop);
这个方法是存在的,而error事件是error handler,专门用来监听error的。
如果process.stdout是异步的,那么可以直接监听到process.stderr,然后通过
stream.write(string, errorhandler);
errorhandler来返回错误信息。
如果process.stdout是同步的且在执行的时候存在运行栈溢出的情况,这种时候通过监听'error'事件可以判断是否是运行栈溢出,因为stream.once
是event emitter,所以回调noop会等到主loop执行完后执行。如果运行栈溢出,noop无法被触发,会抛错并走到catch流程中,而在catch中,如果是第一次运行栈溢出,会通过try来拿到运行栈溢出错误的message:
if (MAX_STACK_MESSAGE === undefined) {
try {
// eslint-disable-next-line no-unused-vars
function a() { a(); }
} catch (err) {
MAX_STACK_MESSAGE = err.message;
}
}
之后,通过对比第一次try的e.message和运行栈溢出的错误信息来抛出错误:
if (e.message === MAX_STACK_MESSAGE && e.name === 'RangeError')
throw e;
from issue-blog.
你说的对于write函数中对于错误处理这块我是很认同的,但对于错误捕获的部分和process.stdout.write是异步还是同步好像没有什么很直接的关联吧,还是说我没有完全get到你想要表达“我阐述的console这块有问题的点”....
from issue-blog.
……
但是,这段代码运行的时候,浏览器可能会认为需要把控制台I/O 延迟到后台,在这种情况下,等到浏览器控制台输出对象内容时,a.index++ 可能已经执行,因此会显示{ index: 2 }。
你说的这句话,应该是不会发生的,因为在执行这个函数console.log(a)
的时候,a就已经固定了,引用之前我说过的那句话:
在调用process.stdout.write之前已经拼接好string了
只不过在输出到控制台的时候可能会在a.index++后面,因为输出是用的process.stdout.write。
from issue-blog.
上面评论中你引用的那个描述:
但是,这段代码运行的时候,浏览器可能会认为需要把控制台I/O 延迟到后台,在这种情况下,等到浏览器控制台输出对象内容时,a.index++ 可能已经执行,因此会显示{ index: 2 }。
是针对js中的console.log来说的,并不是针对node中的console.log,只有node中的console.log才是用的process.stdout.write来进行输出
from issue-blog.
额,好吧,我读下来感觉你在说node。
from issue-blog.
@xtx1130 chrome中console.log的解释是直接摘自《你不知道的JavaScript》的。不过很感谢和你交流那么多,感觉你研究得很深~
from issue-blog.
@SunShinewyf 哦哦,YDK js 那本书是么,我还真没仔细看过。。。有时间摘出来翻翻。不过chrome源码确实没看过。
from issue-blog.
from issue-blog.
@SunShinewyf 哈哈,我之前star过一个YDK JS
from issue-blog.
应该是一样的,只是译本吧,哈哈
from issue-blog.
Related Issues (20)
- 脚手架之egg-init
- node中的Event模块(上) HOT 7
- node中的Event模块(下)
- node中的模块
- Promise 总结
- dva学习总结
- dva 学习总结
- redux-saga 初识
- React 更新组件的源码解读
- Immutable 操作在 React 中的实践
- React 高阶组件浅析
- React 简洁代码之道
- 双向数据绑定的原理和实现 HOT 2
- react-router 源码解读
- 可视化方向第一篇(G2 学习)
- React Hooks 尝鲜
- React Hooks 进阶 HOT 2
- 各大 Form 大揭秘 -- antd Form
- webpack HMR 折腾记 HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from issue-blog.