stevenyuysy / blog Goto Github PK
View Code? Open in Web Editor NEW我只想学好基础前端知识
我只想学好基础前端知识
最近有一种深深的迷茫感,有很多事情要做但是不知道从哪一步开始。最近前端变的越快,自己学习和充电的时间却越来越少,自身所在的业务变更也比较频繁,没有机会和时间深入学习。感觉找个时间梳理一下,从小小的一步开始坚持吧。
最近在整理前端技术信息的获取,一方面是发现以前关注的一些博客更新不那么频繁,一方面是发现自己的列表越来越多,平时根本没有时间看完。还有一个原因是刚好同事在我的整理列表下留言了,让我更新一下。于是想要对获取信息流做一下筛选,做一个列表,不求列全,但求能涵盖到高质量的博客和社区文章。
国外:
国内:
国内:
国外:
国内:
如何筛选出好的博客和社区?以下列了几个维度参考:
TBD...
Philip Roberts: What the heck is the event loop anyway? | JSConf EU 2014 是一篇很经典的关于 event loop 的文章,通俗易懂地解释了 JavaScript Runtime 和外部 web API (DOM, HTTP Request, setTimeout) 是如何通过 EventLoop 连接起来的。
在讲 EventLoop 之前讲一下几个概念,分别是
The Call Stack is a data structure which records basically where in the program we are.
If we step into a function, we put it on the top of the stack. If we return from a function, we pop off the top of the stack. That’s all the stack can do.
当我们进入函数时,我们将其推入栈中。如果我们从函数里返回,我们就会将这个函数弹出。这就是函数调用栈所做的事情。
如果我们调用的一个函数不停的调用自身,就会导致栈里的函数不断暴增直至报错。
假如我们同步发起 HTTP 请求的话,假设这个时候网络状况不好,需要等待很久(例如三秒)才能完成所有的 HTTP 请求,那么此时这个 HTTP 请求的执行方法就会一直在 JS 的调用栈里。我们会发现这个时候整个网页都停止了响应,既不能渲染,也无法响应任何事件(因为调用栈里这个函数还没有执行完,未清空)。直到这个 HTTP 请求结束之后,才会继续执行下面的 JS 程序。
显然如果在一直同步调用一些阻塞进程的复杂运算代码并不会带来好的用户体验,怎么做才能避免这种情况呢,于是就有了异步回调。
那如果我们用 setTimeout 做一个异步操作的话,思考下面的代码,会发生什么?
调用栈里面神奇的消失了?
在 JavaScript Runtime 里面是没有所谓 DOM,HTTP Request,setTimeout 这些方法的实现的,也就是说这些 API 都是由宿主环境(浏览器,NodeJS)提供的。那么这些 API 是怎么通过 JS 调用的呢?
那么究竟 EventLoop 在里面扮演什么样的角色呢?
JavaScript 确实是单线程,且只有一个调用栈,但是 JavaScript 可以通过任务队列将异步的回调管理起来,并且在堆栈清空的时候,逐步处理异步的回调,如同上面的图一样。
由于视频是在 2014 年出来的,之后在 ES6 ES7 引入各种新的 API,假如 Promise 也参与进来之后会发生什么呢?还记的 Web API 调用时有没有优先级呢?
先来看 HTML 5规范 是怎么定义的
每个 task 都有自己相关的 document,比如一个 task 在某个 element 的上下文中进入队列,那么它的document 就是这个 element 的 document。
每个 task 定义时都有一个 task source,从同一个 task source 来的 task 必须放到同一个 task queue,从不同源来的则被添加到不同队列。
每个(task source对应的) task queue 都保证自己队列的先进先出的执行顺序,但 event loop 的每个 turn,是由浏览器决定从哪个 task source 挑选task。这允许浏览器为不同的 task source 设置不同的优先级,比如为用户交互设置更高优先级来使用户感觉流畅。
来看下面这段代码
console.log('script start');
setTimeout(function() {
console.log('setTimeout');
}, 0);
Promise.resolve().then(function() {
console.log('promise1');
}).then(function() {
console.log('promise2');
});
console.log('script end');
输出结果为
script start
script end
promise1
promise2
setTimeout
前面两行同步执行的代码并没有问题,奇怪的是同样都是异步代码,为什么看起来 Promise
的优先级会比 setTimeout
高呢?
为了理解这个问题,我们引入了 tasks 和 Microtasks 的概念。
callback
需要 schedule task,解析 html
,setTimeout
等等这些也一样。Once a promise settles, or if it has already settled, it queues a microtask for its reactionary callbacks. This ensures promise callbacks are async even if the promise has already settled. So calling .then(yey, nay) against a settled promise immediately queues a microtask. This is why promise1 and promise2 are logged after script end, as the currently running script must finish before microtasks are handled. promise1 and promise2 are logged before setTimeout, as microtasks always happen before the next task.
所以一旦 promise
开始 settles,马上就进入 Microtasks 队列中,这样就保证了 Promise 是异步执行的。所以调用 .then()
的时候实际上是进入 Microtasks 的队列里。这也就是为什么 promise1
promise2
要比 script end
出现的晚。因为当前脚本还没执行完毕,microtasks 还未开始执行。而 promise1
promise2
要比 setTimeout
快,则是因为 microtasks 总是在下一个 tasks 之前就开始执行。
小结:
script start
run script
script start
setTimeout callback
Promise then
script end
run script
执行完毕Microtasks
里的队列回调promise1
promise2
setTimeout
其他参考:
之前偶然看到 event loop 相关的东西,我们说 JavaScript 自身是单线程的,但是为了处理异步任务,有了所谓 event loop
的东西,而在 Node.js 里,为了满足特定的需求和浏览器又有不一样的实现方法。简单来讲:
参考:
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.