【发布时间】:2020-09-10 21:19:26
【问题描述】:
我已经阅读了一些关于 async/await 的文档,并尝试通过一个示例来更好地理解它。我所期待的是,下面没有 async 和 await 的代码将首先打印字符串“Completed”,然后是文件的内容。但即使在添加了 async 和 await 之后,我也看到打印顺序没有受到影响。我的印象是异步的,在这种情况下等待使用将首先打印文件内容,然后是字符串“已完成”。
var fs = require('fs');
getTcUserIdFromEmail();
async function getTcUserIdFromEmail( tcUserEmail ) {
let userInfo = {};
let userFound = false;
// Read the file that is containing the information about the active users in Teamcenter.
await fs.readFile('tc_user_list.txt', function(err, data) {
if( err )
console.log( err );
else
console.log( data.toString() );
});
console.log( 'Completed method');
}
请你指出我做错了什么。
谢谢, 帕万。
【问题讨论】:
-
你认为
await是做什么的?你为什么要把它和回调混在一起? -
fs.readFile()不返回承诺。 Using filesystem in node.js with async / await -
await只有在等待承诺时才会做任何有用的事情。fs.readFile()的常规版本不返回承诺,因此等待它没有任何用处。您可以使用fs.promises.readFile(),它将返回一个承诺并且不接受回调。结果来自等待的承诺,而不是通过回调。 -
@GuyIncognito 它使用 fs.promises 工作,如另一个线程中所述。谢谢。
-
fs.promises.readFile()不接受回调。看the documentation。结果或错误会在 Promise 中返回,而不是在回调中。如果你向它传递一个回调,该回调将永远不会被调用。当您使用 Promise 进行编程时,您会在 Promise 中返回您的结果或错误。当你等待一个承诺时,你会得到const data = await fs.promises.readFile(someFile)的结果,你会用try/catch捕获错误。
标签: node.js async-await