在javascript中我们了解到了setTimeout和setInterVal函数事件队列(任务队列)的相关知识,除了setTimeout和setInterval这两个方法外,Node.js还提供了另外两个与"任务队列"有关的方法:process.nextTick和setImmediate。它们可以帮助我们加深对"任务队列"的理解。

  setTimeout()

  首先我们看看setTimeout(setInterVal和setTimeout函数区别只是执行次数)函数,需要注意的是,setTimeout()只是将事件插入了"任务队列",必须等到当前代码(执行栈)执行完,主线程才会去执行它指定的回调函数。要是当前代码耗时很长,有可能要等很久,所以并没有办法保证,回调函数一定会在setTimeout()指定的时间执行。

 setTimeout(function(){console.log('0')},0);//意思是回调函数加入事件队列的队尾,主线程和事件队列的函数执行完成之后立即执行定时器的回调函数,如果定时器的定时是相同的,就按定时器回调函数的先后顺序来执行。
console.log(1);
setTimeout(function(){console.log(2);},1000);
setTimeout(function(){console.log(4);},1000);
console.log(3);
//1 3 0 2 4

  setImmediate()

  setImmediate()是将事件插入到事件队列尾部,主线程和事件队列的函数执行完成之后立即执行setImmediate指定的回调函数,和setTimeout(fn,0)的效果差不多,但是当他们同时在同一个事件循环中时,执行顺序是不定的。另外setTimeout和setImmediate也有一定的区别(还在探索...),在实际工作中我们其实她们的区别貌似不明显,请高手指出感激不尽

 console.log('1');

 setImmediate(function () {
console.log('2');
}); setTimeout(function () {
console.log('3');
},0); process.nextTick(function () {
console.log('4');
}); //1 4 2 3也可能是1 4 3 2

  process.nextTick()

  process.nextTick()方法可以在当前"执行栈"的尾部-->下一次Event Loop(主线程读取"任务队列")之前-->触发process指定的回调函数。也就是说,它指定的任务总是发生在所有异步任务之前,当前主线程的末尾。(nextTick虽然也会异步执行,但是不会给其他io事件执行的任何机会)

 process.nextTick(function A() {
console.log(1);
process.nextTick(function B(){console.log(2);});
}); setTimeout(function C() {
console.log(3');
}, 0)
// 1
// 2
// 3

当然这样也是一样的:

setTimeout(function C() {
console.log(3');
}, 0)
process.nextTick(function A() {
console.log(1);
process.nextTick(function B(){console.log(2);});
});
// 1
// 2
// 3

当然这样还是一样的:

setTimeout(function C() {
console.log(3');
}, 0)
process.nextTick(function A() {
process.nextTick(function B(){console.log(2);});
console.log(1);
});
// 1
// 2
// 3

最后process.maxTickDepth()的缺省值是1000,如果超过会报exceed callback stack。官方认为在递归中用process.nextTick会造成饥饿event loop,因为nextTick没有给其他异步事件执行的机会,递归中推荐用setImmediate

foo = function(bar) {
console.log(bar);
return process.nextTick(function() {
return f(bar + 1);
});
};
setImmediate(function () {
console.log('1001');
});
foo(1);//注意这样不会输出1001,当递归执行到1000次是就会报错exceed callback stack,
/*
foo = function(bar) {
console.log(bar);
   if(bar>1000){
return;
}
return process.nextTick(function() {
return f(bar + 1);
});
};
setImmediate(function () {
console.log('1001');
});
foo(1);
*/

setTimeout和setImmediate以及process.nextTick的区别的更多相关文章

  1. 细说setTimeout/setImmediate/process.nextTick的区别

    node.js中的非IO的异步API提供了四种方法,分别为setTimeOut(),setInterval(),setImmediate()以及process.nextTick(),四种方法实现原理相 ...

  2. setTimeout,setInterval,process.nextTick,setImmediate in Nodejs

    Nodejs的特点是事件驱动,异步I/O产生的高并发,产生此特点的引擎是事件循环,事件被分门别类地归到对应的事件观察者上,比如idle观察者,定时器观察者,I/O观察者等等,事件循环每次循环称为Tic ...

  3. Node.js学习笔记:setImmediate与process.nextTick

    通过process.nextTick注册的函数在当前这个事件循环中执行的函数执行完毕后立即执行,相当于把当前的同步代码执行完毕之后,立刻执行所有的通过process.nextTick注册的函数,如果注 ...

  4. 解读setTimeout, promise.then, process.nextTick, setImmediate的执行顺序

    最近在看<Node.js调试指南>的时候遇到有意思的几道题,是关于setTimeout, promise.then, process.nextTick, setImmediate的执行顺序 ...

  5. Node.js Event Loop 的理解 Timers,process.nextTick()

    写这篇文章的目的是将自己对该文章的理解做一个记录,官方文档链接The Node.js Event Loop, Timers, and process.nextTick() 文章内容可能有错误理解的地方 ...

  6. setTimeout和setImmediate到底谁先执行,本文让你彻底理解Event Loop

    笔者以前面试的时候经常遇到写一堆setTimeout,setImmediate来问哪个先执行.本文主要就是来讲这个问题的,但是不是简单的讲讲哪个先,哪个后.笼统的知道setImmediate比setT ...

  7. Node.js中Process.nextTick()和setImmediate()的区别

    一.Webstrom使用node.js IDE的问题 在区别这两个函数之前来说一下Webstrom使用node.js IDE的问题,在配置Node.js的IDE了,但setImmediate().re ...

  8. process.nextTick,Promise.then,setTimeout,setImmediate执行顺序

    1. 同步代码执行顺序优先级高于异步代码执行顺序优先级: 2. new Promise(fn)中的fn是同步执行: 3. process.nextTick()>Promise.then()> ...

  9. 比较setImmediate(func),setTimeout(func),process.nextTick(func)

    node中的事件优先级机制: console.log('第一笔!'); process.nextTick(function() { console.log('吃个饭吧!'); setImmediata ...

随机推荐

  1. Java 时间转换问题总结

    这几天开发中遇到时间转换出错的问题,特总结如下:   ========================================================================= ...

  2. how to learn device driver

    making a linux usb driver http://www.kroah.com/linux/ http://matthias.vallentin.net/blog/2007/04/wri ...

  3. Codeforces Wilbur and Array

    Description Wilbur the pig is tinkering with arrays again. He has the array a1, a2, ..., an initiall ...

  4. HDU 1226 BFS

    注意密码位数<=500 输出注意十六进制改成字母 要点题目都已说明 ac就好 #include<iostream> #include<stdio.h> #include& ...

  5. web.xml(spring/spring mvc/hibernate)

    <?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" ...

  6. Java循环语句 for

    语法: 特点:相比 while 和 do...while 语句结构更加简洁易读 例如,输出 1000 遍"我爱慕课网",使用 for 的实现代码为: 需要留心的几个小细节: 1. ...

  7. GMT-Note 基本参数详细说明

    http://blog.sciencenet.cn/blog-381041-897592.html 控制经纬度标示中是否带N或者W PLOT_DEGREE_FORMAT    = ddd:mm:ss ...

  8. 重金悬赏的微软:提交Win8漏洞以及发布Win8应用

    随着Windows 8.1这个饱受诟病的操作系统的推出,微软想一举改变颓势,也只有从用户体验上下手了. 近来,微软针对Windows 8.1的漏洞,推出了三项奖励措施: 1.对于发现关键性漏洞,并且这 ...

  9. (10.09作业)学生选课数据库SQL语句练习题

  10. ModSecurity CRS笔记[转]

    转自:http://danqingdani.blog.163.com/blog/static/186094195201472304841643/ ModSecurity的规则因为奇怪的正则(可读性差? ...