Await: CommonJS
Edit on Github
Example of how top-level await can be used by default in Deno. This example would assist in migrating from NodeJS (CommonJS) to Deno.
This example is what you may be used to with NodeJS when using CommonJS modules. Notice that for "await" to be used in this example, it must be wrapped in an "async" function.
./node-await.ts
const fs = require("node:fs");
async function readFile() {
try {
const data = await fs.promises.readFile("example.txt", "utf8");
console.log(data);
} catch (err) {
console.error(err);
}
}
readFile();
This is the same example as above, but with Deno. Notice that as well as being able to use "await" outside of an "async" function, we can also make use of Deno's Filesystem API.
./deno-await.ts
try {
const data = await Deno.readTextFile("example.txt");
console.log(data);
} catch (err) {
console.error(err);
}
Run this example locally using the Deno CLI:
deno run https://docs.deno.com/examples/scripts/top_level_await.ts/main