I have a Javascript async/await design question.
Imagine this function exists:
async function readFile(filename) {
// Asynchronously reads file and returns the content as string
}
Now if I were to use readFile
with a certain parameter and return its result, I could do it either like so:
async function readSettings1() {
const settings = await readFile("settings.json");
return settings;
}
or:
async function readSettings2() {
return readFile("settings.json");
}
I understand both approaches work similarly, and must be invoked identically. They both return Promises. There are a couple of syntactic differences:
readSettings1
first resolves the value ofreadFile
and then returns the hard result. Here, the keywordasync
is used in the function definition so I can useawait
.readSettings2
is really just a passthrough. In fact, I can omitasync
from this function definition.
My question is: are there any semantic differences between readSettings1
and readSettings2
? Do they behave differently in any way? Do you have a preference for either style? Why? Have you read any documentation recommending one style over the other?