Javascript NodeJS异步函数不起作用

Javascript NodeJS异步函数不起作用,javascript,node.js,function,asynchronous,Javascript,Node.js,Function,Asynchronous,我读了很多关于如何在NodeJS中创建异步函数的帖子,但是我想不出来!我知道这是问得最多的主题,但请看下面的示例代码: function test2(){ console.log("Check x"); } function test(callback){ for(var i=0;i<1000000000000;i++){} callback(); } console.log("Check 1"); test(test2); console.log("Check 2");

我读了很多关于如何在NodeJS中创建异步函数的帖子,但是我想不出来!我知道这是问得最多的主题,但请看下面的示例代码:

function test2(){
console.log("Check x");
}
function test(callback){
    for(var i=0;i<1000000000000;i++){}
    callback();
}

console.log("Check 1");
test(test2);
console.log("Check 2");
console.log("Check 3");
函数test2(){
控制台日志(“检查x”);
}
函数测试(回调){

对于(var i=0;iNode.js中并非所有内容都是异步的。 异步过程仅在涉及I/O或事件时发生,如访问文件系统、处理网络请求、从数据库读取数据等

例如:

var fs = require('fs); //node.js built-in file system which requires I/O from storage
function getDataFromFile(callback) {
  //fs.readFile is asynchronous process
  fs.readFile('path/to/file', (err, data) => {
    if (err) throw err;
     callback(data);
  });
}
getDataFromFile(function(data) {
  //this is asynchronous callback from getDataFromFile()
  console.log('data ' + data);
});

并非Node.js中的所有内容都是异步的。 异步过程仅在涉及I/O或事件时发生,如访问文件系统、处理网络请求、从数据库读取数据等

例如:

var fs = require('fs); //node.js built-in file system which requires I/O from storage
function getDataFromFile(callback) {
  //fs.readFile is asynchronous process
  fs.readFile('path/to/file', (err, data) => {
    if (err) throw err;
     callback(data);
  });
}
getDataFromFile(function(data) {
  //this is asynchronous callback from getDataFromFile()
  console.log('data ' + data);
});

使用
settimeout
代码中没有异步。使用
settimeout
代码中没有异步。