Comments (7)
@toBeTheLight 正好watch这个blog,看到了你的问题,第二个问题我可以回答你:
setTimeout(fn,time)
这里面的fn是以相同的time为基准,把fn存储到一个双向链表中。当:
setTimeout(() => setTimeout(fn,0),0)
在运行event-loop的timers阶段的时候:
外层的setTimeout会执行此时刻链表里面的所有fn,而只有在执行到外层的fn(即() => setTimeout(fn,0)
)的时候,才能把内部的fn注册到setTimeout中,所以会移到下一个event-loop的timers阶段运行。但是如果内层是setImmediate的时候,会把setImmediate注册到check阶段,而此轮event-loop的check阶段尚未运行,所以会放到此轮的event-loop中运行。有兴趣的话可以看一下timer.js的源码了解一下原理
from issue-blog.
学姐 setTimeout(fn,0) Vs setImmediate 到底是随机的还是setImmediate在后面呢? 我这里的结果有点晕。。
from issue-blog.
setTimeout(fn,0) Vs setImmediate
的执行顺序是随机的,具体要看两者是否在一个I/O循环中进行调用,如果在,setImmediate
始终会比 setTimeout
先执行。因为setImmediate
会在 event loop
中poll
完成之后立即执行,setTimeout
则是到下一个 timers
阶段。
还有,你后面的打印结果是我上面的例子吗
from issue-blog.
嗯 懂了 这个结果是你上面的例子
from issue-blog.
因为setTimeout(fn,0) Vs setImmediate
的结果可能是随机的,所以就会出现有的时候setImmediate
在setTimeout(fn,0)
之前执行,有的时候在setTimeout(fn,0)
之后执行
from issue-blog.
你好,我有两个疑问。
- node和浏览器的不同。在循环过程中,node中每个阶段的任务是一次性拿出,执行完毕后再清空执行microtask/nextTick,再进行下个阶段,而浏览器是执行一个macrotask,就执行清空microtask/nextTick,再进行下个macrotask任务,是这样吗?
- 比如说我在timer阶段执行setTimeout过程中创建的setTimeout是进入下轮loop吗,还是直接放入当前阶段?(我知道如果执行timer时创建了check,肯定会优先放入当前loop,有这个疑问主要是写了很多测试代码,不知道是不是哪里搞错了,过一会执行结果就变了)
from issue-blog.
setImmediate属于check观察者没错,但是并不是每次只执行链表中的一个回调函数,而是一次取出全部执行完:
setImmediate(() => {
console.log('setImmediate1');
process.nextTick(() => {
console.log('nextTick');
});
});
setImmediate(() => {
console.log('setImmediate2');
});
setImmediate(() => {
console.log('setImmediate3');
});
nextTick是最后输出的。
from issue-blog.
Related Issues (20)
- 脚手架之egg-init
- node中的Event模块(下)
- node中的模块
- node中的process模块 HOT 12
- 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.