My question has to do with the await
purpose in node.js
. I'm going to write some dummy code in order to express my question, so please don't mind if there's something missing:
let f = async function(x) {
var data = await readFile('x.txt');
var link = openLink(`./myfile/${data}`);
return link;
}
So, as I understand it, f
will run in a separate thread from the Nodejs Event Loop while it expects the returning 'promised' value of link
. When that new thread opens, the function gets executed synchronously within that thread - but if that's true, why bother with the await
? I mean, the function content is executed from top to bottom, like in any synchronous programming language, so it already naturally "waits" for readFile
to complete in order to continue to the next line and use data
as a parameter.
I'm surely misunderstanding something here, but as I read articles and documentation about async
await
it doesn't get clearer for me why the await
must be there.