对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.mzph.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 降低填充率技巧…