大佬们看看下面代码会怎么样?

新手上路,请多包涵
const fs = require('fs');

function writeFile() {
    fs.writeFile('a.txt', 'hello', { flag: 'a' }, err => {
        if (err) return console.log(err);
        console.log('ok');
    });
}

function name() {
    writeFile();
    while (1) { }
}

name();
  1. 创建并写入'hello',打印'ok'
  2. 创建并写入'hello',没有打印
  3. 创建但没有写入'hello',没有打印
  4. 不创建,没有打印

各位看看会是哪种情况呢?为什么?

阅读 1.5k
2 个回答

创建但没有写入'hello',没有打印
马上创建,等待异步写入,
回到主流程,while(1) 无限循环,然后就死了。

第二个,这跟定时器后面跟一个while(true) 的那种题有什么区别么

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题