console.log("---------Event Order---------");
const fs = require('fs');
const path = require('path');
const currentDir=__dirname
const fileName='example.txt'
const filePath=path.join(currentDir,fileName)
console.log('Start--------');
setTimeout(()=>{
console.log('setTimeout 1');
},0)
setImmediate(()=>console.log('setImmediate 2'))
setImmediate(()=>console.log('setImmediate 3'))
setTimeout(()=>{
console.log('setTimeout 4');
},0)
fs.readFile(filePath,(err,data)=>{
if (err) {
console.log('Error while reading file',err);
}else{
console.log(data.toString());
}
})
console.log('end--------');
this is the code spinet and the order of log get differ from the official doc
statement:“However, if you move the two calls within an I/O cycle, the immediate callback is always executed first:”
my log as below
Start——–
end——–
setTimeout 1
setTimeout 4
setImmediate 2
setImmediate 3
Hello, this is some content!