Javascript with Chorme v8 engine works like this :

For Chorme engine, v8, it has call stack.

And all the async opreations functions are stay in webapis. So everytime  you call 'setTimeout()' or http call, it will always call webapis.

So, in the picture,

we call the main() function, and push into the call stack;

then console.log("Hi"); put into call stack and finish then poped up from the call stack;

third, setTimeout() function run into the call stack, then it call webapis with callback function, so push into webapis. And setTimeout() popup from the call stack.

fourth, console.log('JSC'); push into the call stack and poped up.

Finally main() finished and poped up:

So what left here is just webapis have a callback function, waiting 5 second, when the time out, it was push to the task queue, NOT to the call stack.

Now the 'Event loop' come in, what event loop does is: it check whether there is any task in the call stack.... if yes, finish those functions in the calll stack first. If not, then event loop checks the task queue then push the callback from the task queue to call stack.

Then console.log() function run and poped up from the call stack.

-----------------------------------------

You might have met the problem that you run setTimeout(fn(), 0); it wait no time, but the fn() come at last:

function one(){
console.log("One");
} function two(){
console.log("Two");
} function delay(){
console.log("Delay");
} one();
setTimeout(delay, 0);
two(); /*
"One"
"Two"
"Delay"
*/

That is because, setTimeout is webapis, so the callback function delay() will be pushed into the 'task queue'. It will wait all the functions in 'call stack' finish, then even loop will check the task queue and push the callback to the call stack. So the "Delay" console log out at the bottom.

Link: https://www.youtube.com/watch?v=8aGhZQkoFbQ

[Javascript] Task queue & Event loop.的更多相关文章

  1. JavaScript事件循环(Event Loop)机制

    JavaScript 是单线程单并发语言 什么是单线程 主程序只有一个线程,即同一时间片断内其只能执行单个任务. 为什么选择单线程? JavaScript的主要用途是与用户互动,以及操作DOM.这决定 ...

  2. 详解JavaScript中的Event Loop(事件循环)机制

    前言 我们都知道,javascript从诞生之日起就是一门单线程的非阻塞的脚本语言.这是由其最初的用途来决定的:与浏览器交互. 单线程意味着,javascript代码在执行的任何时候,都只有一个主线程 ...

  3. JavaScript 事件循环 — event loop

    引言 相信所有学过 JavaScript 都知道它是一门单线程的语言,这也就意味着 JS 无法进行多线程编程,但是 JS 当中却有着无处不在的异步概念 .在初期许多人会把异步理解成类似多线程的编程模式 ...

  4. 一文梳理JavaScript 事件循环(Event Loop)

    事件循环(Event Loop),是每个JS开发者都会接触到的概念,但是刚接触时可能会存在各种疑惑. 众所周知,JS是单线程的,即同一时间只能运行一个任务.一般情况下这不会引发问题,但是如果我们有一个 ...

  5. 从Javascript单线程谈Event Loop

    假如面试回答js的运行机制时,你可能说出这么一段话:"Javascript的事件分同步任务和异步任务,遇到同步任务就放在执行栈中执行,而碰到异步任务就放到任务队列之中,等到执行栈执行完毕之后 ...

  6. 深入理解Javascript单线程谈Event Loop

    假如面试回答js的运行机制时,你可能说出这么一段话:"Javascript的事件分同步任务和异步任务,遇到同步任务就放在执行栈中执行,而碰到异步任务就放到任务队列之中,等到执行栈执行完毕之后 ...

  7. JavaScript 运行机制 (Event Loop)

    单线程就意味着,所有任务需要排队,前一个任务结束,才会执行后一个任务.如果前一个任务耗时很长,后一个任务就不得不一直等着. 所有任务可以分成两种,一种是同步任务(synchronous),另一种是异步 ...

  8. Javascript之Event Loop

    先看段代码: console.log(1); setTimeout(function () { console.log(2); new Promise(function (resolve, rejec ...

  9. JavaScript 运行机制详解:Event Loop

    参考地址:http://www.ruanyifeng.com/blog/2014/10/event-loop.html 一.为什么JavaScript是单线程? JavaScript语言的一大特点就是 ...

随机推荐

  1. Java 测试Hibernate+Mysql简单的数据存储

    想使用Hibernate框架,在网上看了一个Hibernate学习视频,试着做了一个小小的Java连接数据库的操作,Java初学者一个,大家多多包涵 开发环境: 1.安装MySql, 2.安装了Ecl ...

  2. Swift 字符串连接

    // 使用+直接相加 var i = var str = "Hello" str = str + " jinpangpang" // 可以使用这种方式连接整值 ...

  3. php多文本框提交

    <form action="" method="post"><input type=text name=name[]><input ...

  4. Codeforces Round #278 (Div. 1)

    A A monster is attacking the Cyberland! Master Yang, a braver, is going to beat the monster. Yang an ...

  5. C# 汉子增加UTF-8头

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Conv ...

  6. POJ3253 Haffman

    POJ3253 分析: 简单的哈弗曼树的应用. AC代码: //Memory: 316K Time: 16MS #include <iostream> #include <cstri ...

  7. libevent入门(1)

    libevent是什么                libevent 库实际上没有更换 select().poll() 或其他机制的基础.而是使用对于每个平台最高效的高性能解决方案在实现外加上一个包 ...

  8. .cshrc

    使用set和setenv命令可以设置shell选项或者列出shell变量 在C Shell 里, set 定义局部变量, setenv定义全局变量   1.set   set 变量名 = 内容: s2 ...

  9. PowerShell 字符串操作符

    字符串操作符 格式化操作符 –F 在PowerShell文本操作符中非常重要,经常被用来增强数字类型和日期类型的可读性: "{0} diskettes per CD" -f (72 ...

  10. quote, quasiquote, unquote和unquote-splicing

    关于符号类型 符号类型又称引用类型,在概要一文中本人介绍得非常的模糊,使很多初学者不理解.符号类型在Scheme语言中是最基础也是最重要的一种类型,这是因为Scheme语言的祖先Lisp语言的最初目的 ...