How does a single thread handle asynchronous code in JavaScript?

原文:https://www.quora.com/How-does-a-single-thread-handle-asynchronous-code-in-JavaScript

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

Well, arguably its not true that Javascript is single threaded if you see from the under hood working of browser JS to your JS code, there are thread pools. By single threaded what they mean(browser end) is your JS runs into a single threaded event loop. There is one single thread that handles your event loop. Under your JS, the browser code is running multiple threads to capture events and trigger handlers, when they capture any new event, they push it on an event queue and then that event loop, in which your code is running gets triggered and it handles the request e.g. It performs an action which can be to show a DIV, which again triggers the Browser to print it, which in turn runs a thread to do it(from the thread pool).

Lets take an example of your JS algo.
window.onload 
     Show Header
     Send an Ajax Req. for config - > When done, alert a box
     Do some more page animations
So when you told the browser to send an ajax request with a provided callback, it saves it in memory and this Network IO call is transferred to a thread in Network Pool Threads, your code next to that line will continue to work. After the Network Call thread has done its job, it will push on the event queue the response. Remember that event loop? Here is when it comes to action, it picks the top entity and then trigger your callback(Context Switching on CPU Level), which in turn could be anything. Now try doing something like this.
window.onload
     Show Header
     Send an Ajax req. for config -> 
     When done -> Trigger another Ajax 
         -> for loop 0.100000000
     Do more animation

Now here, if even your second Ajax completes in due time, its callback will not be triggered until your for loop exits. Its a sort of blocking within the thread, none of the event will be fired and everything will be frozen!

Hope that clears your doubt.

Cheers!

原文地址:https://www.cnblogs.com/oxspirt/p/9087366.html

时间: 2024-08-12 22:02:28

How does a single thread handle asynchronous code in JavaScript?的相关文章

Android异步处理-Thread+Handle/AsynTask实现异步更新UI

每个Android应用程序都运行在一个dalvik虚拟机进程中,进程开始的时候会启动一个主线程(MainThread),主线程负责处理和ui相关的事件,因此主线程通常又叫UI线程.而由于Android采用UI单线程模型,所以只能在主线程中对UI元素进行操作.如果在非UI线程直接对UI进行了操作,则会报错. 一.Thread+Handle Android为我们提供了消息循环的机制,我们可以利用这个机制来实现线程间的通信.那么,我们就可以在非UI线程发送消息到UI线程,最终让Ui线程来进行ui的操作

javaScript Code 用javascript确定每月第二个星期五

废话少说只就上Code: 说明:getDay()方法获取星期(这里的星期是从0到6).参见:http://www.w3school.com.cn/js/js_obj_date.asp 中的getDay(). 代码有不足之处希望得到指正. var SecondFriday = { getSecondFriday: function () { var flag = 0; //(1) 获取当月月初时间,时间格式为:Sun Jun 01 2014 00:00:00 GMT+0800 (中国标准时间) v

[code style]javascript style

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 var globalVariable=null; var globalObject={     init:function(){         $.extend({method:function(e){e}});    //jquery static method extend         $.fn.extend(); //jquery instance method extend  

Visual Studio Code中JavaScript开发环境的配置

Visual Studio Code简称VS Code,是一款由微软公司免费开源的现代化轻量级代码编辑器,几乎支持所有的主流开发语言的语法高亮.自定义热键.代码片段.括号匹配等诸多特性,还支持插件扩展,并针对网页开发和云端应用开发做了优化.下面简单介绍一下VS Code中JavaScript开发环境的配置. 首先在微软官网上下载安装好VS Code后并打开,在扩展商店中安装插件,直接Ctrl+Shift+X打开扩展商店,在搜索框中搜索Code Runner和Debugger for Chrome

使用Visual Studio Code调试Javascript

Code Runner 在应用商店中搜索Code Runner插件进行安装. 选中你要执行的Javascript脚本,右键选择Run Code,利用Console.log在下方的输出窗口里可以看到输出结果. 如果不小心点击了关闭按钮X关闭了输出窗口可以点击左上方的调试控制台重新打开. 通常我用它快速输出一段JS代码的运算结果. 写算法的时候有时候出岔子Run Code会死循环一直在跑,此时在下方的输出窗口中,右键菜单里选择stop code run即可强制退出. Node.js Node.js?

VS Code创建 JavaScript 运行环境

由于 VS Code 天然支持 JS, 所以上手很简单. 一些插件 coder runner: 本地运行各种代码 color Highlight: 高亮 copy md as HTML: 将 md 转化为 html ES7/ES6: EcmaScript 语法纠错 调试 JS 写好代码后 F5 进入调试,可以看到执行栈/任务队列的信息. 原文地址:https://www.cnblogs.com/Zbhoter/p/12580488.html

VS Code 运行 JavaScript 文件时出现“node...”乱码或错误

1.乱码图片: 2.如果是中文乱码的话,可以到设置里边把「Auto Guess Encoding」这一项勾起来. 3.如果不是这个原因,可能是因为没安装 Node.js 和配置 Node.js 环境,我就是这样子解决乱码问题的.在 windows 下安装和配置 Node.js 环境的教程可见:Node.js安装及环境配置(完整) 希望你的问题能够得到解决. 原文地址:https://www.cnblogs.com/lonelyWMW/p/11680043.html

事件轮询 event loop

Understanding the node.js event loop The first basic thesis of node.js is that I/O is expensive: So the largest waste with current programming technologies comes from waiting for I/O to complete. There are several ways in which one can deal with the

Javascript定时器(三)——setTimeout(func, 0)

setTimeout(func, 0)可以使用在很多地方,拆分循环.模拟事件捕获.页面渲染等 一.setTimeout中的delay参数为0,并不是指马上执行 <script type="text/javascript"> function delay1() { console.log('delay1'); } function delay2() { console.log('delay2'); } function delay3() { console.log('dela