對eventloop的研究

javasctipt是一門單線程的非阻塞的腳本語言,單線程意味著,JavaScript

單線程意味著,javascript代碼在執行的任何時候,都只有一個主線程來處理所有的任務。

JavaScript的事件分兩種,宏任務(macro-task)微任務(micro-task)

宏任務:包括整體代碼script,setTimeout,setInterval

微任務:Promise.then(非new Promise),process.nextTick(node中)

  • 事件的執行順序,是先執行宏任務,然后執行微任務,這個是基礎,任務可以有同步任務和異步任務,同步的進入主線程,異步的進入Event Table并注冊函數,異步事件完成后,會將回調函數放入Event Queue中(宏任務和微任務是不同的Event Queue),同步任務執行完成后,會從Event Queue中讀取事件放入主線程執行,回調函數中可能還會包含不同的任務,因此會循環執行上述操作。

舉個例子

? ??setTimeout(

function()

{

console.log('setTimeout');

},1000)

new Promise(function(resolve)

{ console.log('promise');

}).then(

function() {

console.log('then');

})

console.log('console');

//2,4,3,1.

  • 先執行script同步代碼

    先執行new Promise中的console.log(2),then后面的不執行屬于微任務
    然后執行console.log(4)
  • 執行完script宏任務后,執行微任務,console.log(3),沒有其他微任務了。
  • 執行另一個宏任務,定時器,console.log(1)。




?

轉載于:https://www.cnblogs.com/zhouyideboke/p/10255955.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/278475.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/278475.shtml
英文地址,請注明出處:http://en.pswp.cn/news/278475.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【SSH高速進階】——struts2簡單的實例

近期剛剛入門struts2。這里做一個簡單的struts2實例來跟大家一起學習一下。 本例實現最簡單的登陸,僅包括兩個頁面:login.jsp 用來輸入username與password;success.jsp 為登陸成功頁面。error.jsp為登陸失敗頁面。 1、新建web項目“struts2”…

《智能家居》培訓第六天------2019-01-10

目錄: 一)攝像頭 二)照明 三)所想 四)總結 一)攝像頭 攝像頭這塊學了跟沒學一樣我覺得,攝像頭給的api,yuyv轉rgb24也是給的api,總而言之就是,直接給了兩個源文…

在Linux上按大小列出文件和目錄

This page will show us how to create a list of files and folders ordered by size using standard Linux commands. 該頁面將向我們展示如何使用標準Linux命令創建按大小排序的文件和文件夾列表。 命令 (Command) To get a list with the size of each item in a folder, y…

記一次kafka數據丟失問題的排查

2019獨角獸企業重金招聘Python工程師標準>>> 數據丟失為大事,針對數據丟失的問題我們排查結果如下。 第一:是否存在數據丟失的問題? 存在,且已重現。 第二:是在什么地方丟失的數據,是否是YDB…

Maximum upload size exceede上傳文件大小超出解決

在這里記錄三種方法, 努力提高自己的姿勢水平 application.yml配置spring:servlet:multipart:enabled: truemax-file-size: 10MB #單個文件最大大小max-request-size: 1024MB #上傳數據總大小 application.properties配置spring.servlet.multipart.max-file-size10Mb #單個文件…

ipad iphone開發_如何在iPhone或iPad上更改應用程序的語言

ipad iphone開發BigTunaOnline/Shutterstock.comBigTunaOnline / Shutterstock.comApple’s iOS 13 makes the iPhone and iPad multilingual. Now, you can change the language of an individual app without changing your primary system language. Each app can have its …

Docker最全教程——從理論到實戰(七)

Docker最全教程——從理論到實戰(七) 原文:Docker最全教程——從理論到實戰(七)在本系列教程中,筆者希望將必要的知識點圍繞理論、流程(工作流程)、方法、實踐來進行講解,而不是單純…

Bash Cookbook 學習筆記 【中級】

Read Me 本文是以英文版<bash cookbook> 為基礎整理的筆記&#xff0c;力求脫水2018.01.21 更新完【中級】。內容包括工具、函數、中斷及時間處理等進階主題。本系列其他兩篇&#xff0c;與之互為參考 【基礎】內容涵蓋bash語法等知識點。傳送門【高級】內容涉及腳本安全…

設置Windows 10時如何創建本地帳戶

Windows 10 tries its hardest to make you use a Microsoft account. The option was already hidden, but now it’s not even offered on Windows 10 Home while you’re connected to the internet. Here’s how to create a local account anyway. Windows 10盡最大努力使…

HSQL

Hive的數據存儲  1、Hive中所有的數據都存儲在 HDFS 中&#xff0c;沒有專門的數據存儲格式&#xff08;可支持Text&#xff0c;SequenceFile&#xff0c;ParquetFile&#xff0c;RCFILE等&#xff09;  2、只需要在創建表的時候告訴 Hive 數據中的列分隔符和行分隔符&…

在PowerPoint 2010中將鼠標用作激光筆

Have you ever wished you had a laser pointer to focus attention on a key point in a PowerPoint slideshow? Today, we’ll take a look at how can use use your mouse as a laser pointer in PowerPoint 2010. 您是否曾經希望激光指示器能將注意力集中在PowerPoint幻燈…

Java 8 并發: 原子變量和 ConcurrentMap

原文地址: Java 8 Concurrency Tutorial: Atomic Variables and ConcurrentMap AtomicInteger java.concurrent.atomic 包下有很多原子操作的類。 在有些情況下&#xff0c;原子操作可以在不使用 synchronized 關鍵字和鎖的情況下解決多線程安全問題。 在內部&#xff0c;原子類…

this表示當前對象簡單實例

直接上代碼 class Message { private Channel channel ; // 保存消息發送通道 private String title ; // 消息標題 private String content ; // 消息內容 // 4、調用此構造實例化&#xff0c;此時的channel 主類ch public Message(Channel channel,String title,String cont…

twitter推文不收錄_如何使用Twitter書簽保存推文供以后使用

twitter推文不收錄Khamosh PathakKhamosh PathakTwitter has a new Bookmarks feature that lets you privately save tweets for later. If you’ve been using the Like feature as a workaround for saving tweets, here’s why you should start bookmarking. Twitter具有一…

if的作用域問題 *輸出1~6的隨機數*

1 //測試if語句2 public class TestIf {3 public static void main(String[] args){4 double d Math.random();//0~1之間的小數5 int e (int)(d*5); //[0,4]6 //int f 1(int)(d*6); //[1,6] 擲色子7 System.out.println(e);8 …

為您的Blogger博客設計一個美麗的新主題

Would you like to give your Blogger blog a fresh coat of paint with a new theme? Here’s how you can use the new Template Designer to make your Blogger site stand out from the crowd and look great. 您想給Blogger博客一個新的主題嗎&#xff1f; 您可以通過以…

Lab 6-4

In this lab, we’ll analyze the malware found in the file Lab06-04.exe. Questions and Short Answers What is the difference between the calls made from the main method in Labs 6-3 and 6-4? A: The function at 0x401000 is the check Internet connection method…

步入三十歲前的總結:看似經歷很多得到很多,但,實際卻一無所得

本文算是一篇審視自己的文章吧&#xff0c;感覺跟我類似經歷的人應該很多&#xff0c;認同感應該也大一些。我是12年網絡專業很普通的一所大專院校畢業&#xff0c;到現在為止工作已經超過五年。這五年里&#xff0c;做過運維工程師&#xff0c;也在小車床工作間里做了一下技工…

vue---day03

1. Vue的生命周期 - 創建和銷毀的時候可以做一些我們自己的事情 - beforeCreated - created - beforeMount - mounted - beforeUpdate - updated - activated - deactivated - beforeDestroy - destroyed 1.1 知識點回顧 1.1.1 be…

U Sparkle 開發者計劃招募中!

向我們投稿吧 在此之前&#xff0c;我們有收到過幾篇民間高手的投稿&#xff0c;如&#xff1a; USequencer 初識&#xff08;作者&#xff1a;焱燚(七火)&#xff09; Unity游戲界面解決方案: PSD For UGUI&#xff08;作者&#xff1a;張俊欽&#xff09; UGUI 降低填充率技巧…