我试图理解 Node.js 中的 fs.statSync 命令。我读过这个问题Check synchronously if file/directory exists in Node.js它将检查文件是否同步存在。我不明白这是什么意思,检查它是否同步存在和检查它是否异步存在有什么区别。
最佳答案
同步和异步之间的主要区别是异步不等待。您必须学习如何以全新的方式编写代码,以确保事情按正确的顺序发生。
值得学习如何做到这一点,但一开始可能会有点费脑子。
fs.readFileSync(file); // Stops and waits for completion.
console.log("This will always print after fs.readFileSync.");
fs.readFile(file, function callback(err, data) { // Doesn't stop but will run the callback when it HAS read the file.
if (err) throw err; // File doesn't exist.
console.log(data);
});
console.log("This will print before fs.readFile finishes, probably, it might not, but it might, you never quite know.");
关于javascript - 检查文件是否同步存在是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31433251/