Exercise: Async vs Sync File IO

It's your turn to learn some Deno built-in functions!

Write a Deno program to list the names of the files in the current directory, similar to ls on Mac OS and Linux or dir on Windows.

Look through the Deno Runtime API documentation to find a function to help you.

Scroll to see the answer...











Your code in main.ts:

for await (const dirEntry of Deno.readDir(".")) {
  console.log(dirEntry.name);
}

You could also use the Deno.cwd() function in place of ".". Both represent the path to the current working directory - the folder you are executing deno run from.

Note that Deno.readDir returns an AsyncIterable which is why we use the for await ... of statement. We could also use the synchronous version Deno.readDirSync and use a regular for ... of statement.

This can be run using:

deno run --allow-read main.ts


Have questions or issues you are encountering? Post your questions in our #deno channel on Discord! (see lecture 2 to join the community)