在Node.js中使用setImmediate()和process.nextTick()的意义
Significance of using setImmediate() and process.nextTick() in Node.js
我正在学习Node.js,很难理解setImmediate()
和process.nextTick()
的工作原理。为了清楚地理解这些概念,我有三个程序,一个是简单的node.js程序,另一个是使用setImmediate()
的程序,还有一个使用process.nextTick()
的程序。显然三者的行为方式不同。如果有人能解释一下这些程序在后台操作中的差异,并解释一下使用这两个定时器对有效的Node.js编程的重要性,那将是非常有帮助的。
程序1:(无setImmediate()
和process.nextTick()
)
var fs = require('fs');
function logCar(car, callback){
console.log("Saw a %s", car);
if(cars.length){
callback();
}
}
function logCars(cars){
var car = cars.pop();
logCar(car, function(){
logCars(cars);
});
}
var cars = ["Ferrari", "Porsche", "Bugatti",
"Lamborghini", "Aston Martin"];
logCars(cars);
var fileCheck = function(){
console.log('entered file operation');
fs.stat("fileio.js", function(err,stats){
if(stats)
console.log("file exists");
});
}
fileCheck();
结果:
Saw a Aston Martin
Saw a Lamborghini
Saw a Bugatti
Saw a Porsche
Saw a Ferrari
entered file operation
file exists
程序1:(带setImmediate()
)
var fs = require('fs');
function logCar(car, callback){
console.log("Saw a %s", car);
if(cars.length){
setImmediate(function(){
callback();
});
}
}
function logCars(cars){
var car = cars.pop();
logCar(car, function(){
logCars(cars);
});
}
var cars = ["Ferrari", "Porsche", "Bugatti",
"Lamborghini", "Aston Martin"];
logCars(cars);
var fileCheck = function(){
console.log('entered file operation');
fs.stat("fileio.js", function(err,stats){
if(stats)
console.log("file exists");
});
}
fileCheck();
结果:
Saw a Aston Martin
entered file operation
Saw a Lamborghini
file exists
Saw a Bugatti
Saw a Porsche
Saw a Ferrari
程序2:(带process.nextTick()
)
var fs = require('fs');
function logCar(car, callback){
console.log("Saw a %s", car);
if(cars.length){
process.nextTick(function(){
callback();
});
}
}
function logCars(cars){
var car = cars.pop();
logCar(car, function(){
logCars(cars);
});
}
var cars = ["Ferrari", "Porsche", "Bugatti",
"Lamborghini", "Aston Martin"];
logCars(cars);
var fileCheck = function(){
console.log('entered file operation');
fs.stat("fileio.js", function(err,stats){
if(stats)
console.log("file exists");
});
}
fileCheck();
结果:
Saw a Aston Martin
entered file operation
Saw a Lamborghini
Saw a Bugatti
Saw a Porsche
Saw a Ferrari
file exists
在触发任何其他I/O事件之前运行process.nextTick()
延迟的回调。
使用setImmediate()
,执行将在队列中已经存在的任何I/O事件之后排队。
相关文章:
- 如何同步写入process.stdout
- Node.js-等待process.exit()在前面的代码完成后执行
- 将process.env变量从节点传递或使用到reactjs
- mongoDB插入并处理.nextTick
- 在Node.js中使用setImmediate()和process.nextTick()的意义
- 命令行上的输入在什么时候解析为 node.js 中的 process.argv
- 是什么原因导致process.hrtime()挂在nodejs中
- Express+PassportJs:为什么我们需要在passport策略中延迟process.nextTick()方法
- 节点0.10中未定义Process.domain
- JSON值使用process.env[node.js]动态更新
- “process.nextTick”如何防止我的堆栈爆炸
- 节点服务器响应错误: process.nextTick(function(){throw err;});.
- process.nextTick的常用方法
- 为什么process.nextTick()在这个“假异步函数”中触发了一个TypeError,而setTimeout()
- 为什么以及何时使用process.nextTick
- 什么时候我需要使用process.nextTick在node.js中的最佳实践异步函数
- 什么时候是使用process.nextTick的最佳时机
- process.nextTick(function(){抛出错误;});错误:Can't设置发送后的标头
- 理解node.js事件队列和process.nextTick
- 在process.nextTick中未定义回调