📖 Scenario: You are building a simple Deno script that reads a file and prints its content. Deno requires explicit permission to access files. You will learn how to request permission at runtime before reading the file.
🎯 Goal: Create a Deno script that requests permission to read a file at runtime. If permission is granted, read and print the file content. If permission is denied, print a message saying permission was denied.
📋 What You'll Learn
Create a variable called filePath with the value "./example.txt".
Create a variable called readPermission that requests read permission for filePath using await Deno.permissions.request.
Use an if statement to check if readPermission.state is "granted".
Inside the if, read the file content asynchronously using await Deno.readTextFile and store it in content.
Print the content variable.
In the else block, print "Permission to read the file was denied.".
💡 Why This Matters
🌍 Real World
Many Deno scripts need explicit permission to access files or network resources. This project shows how to ask for permission at runtime, making your scripts safer and user-friendly.
💼 Career
Understanding Deno's permission system is important for developers working with secure scripts or applications that interact with the file system or network.
Progress0 / 4 steps
1
Set the file path variable
Create a variable called filePath and set it to the string "./example.txt".
Deno
Hint
Use const filePath = "./example.txt" to store the file path.
2
Request read permission for the file
Create a variable called readPermission that requests read permission for filePath using await Deno.permissions.request({ name: "read", path: filePath }).
Deno
Hint
Use await Deno.permissions.request with { name: "read", path: filePath } to request permission.
3
Check if permission is granted
Use an if statement to check if readPermission.state is equal to "granted".
Deno
Hint
Use if (readPermission.state === "granted") to check permission.
4
Read and print file content or show denial message
Inside the if block, read the file content asynchronously using await Deno.readTextFile(filePath) and store it in a variable called content. Then print content. In the else block, print the string "Permission to read the file was denied.".
Deno
Hint
Use await Deno.readTextFile(filePath) to read the file and console.log to print.
Practice
(1/5)
1. What does Deno.permissions.query() do in a Deno program?
easy
A. It checks if a specific permission is already granted or denied.
B. It automatically grants all permissions to the program.
C. It revokes permissions from the program at runtime.
D. It lists all permissions the program will request in the future.
Solution
Step 1: Understand the purpose of Deno.permissions.query()
This method is used to check the current status of a permission, whether it is granted, denied, or prompt.
Step 2: Compare with other permission methods
Unlike request() which asks for permission, query() only checks the status without changing it.
Final Answer:
It checks if a specific permission is already granted or denied. -> Option A
Quick Check:
Permission check = A [OK]
Hint: Query checks status without changing permissions [OK]
Common Mistakes:
Confusing query() with request()
Thinking query() grants permissions
Assuming query() revokes permissions
2. Which of the following is the correct syntax to request read permission for the file data.txt in Deno?
easy
A. await Deno.permissions.request("read", "data.txt");
B. Deno.permissions.request({ permission: "read", file: "data.txt" });
C. await Deno.permissions.request({ name: "read", path: "data.txt" });
D. Deno.permissions.query({ name: "read", path: "data.txt" });
Solution
Step 1: Identify correct method and parameters
The request() method takes an object with a name property for permission type and optional path for file-specific permission.
Step 2: Check syntax correctness
await Deno.permissions.request({ name: "read", path: "data.txt" }); uses correct async call with an object containing name: "read" and path: "data.txt". Other options misuse parameters or use query instead of request.
Final Answer:
await Deno.permissions.request({ name: "read", path: "data.txt" }); -> Option C
Quick Check:
Correct request syntax = B [OK]
Hint: Use object with name and path in request() [OK]
Common Mistakes:
Passing parameters as separate arguments
Using query() instead of request()
Wrong property names like permission or file
3. Consider this code snippet in Deno:
const status = await Deno.permissions.query({ name: "net" });
console.log(status.state);
If the program has not requested network permission yet, what will be printed?
medium
A. "granted"
B. "denied"
C. An error is thrown
D. "prompt"
Solution
Step 1: Understand default permission state
When a permission like "net" is queried before any request, Deno returns the state "prompt" indicating user has not yet granted or denied.
Step 2: Confirm no error thrown on query
Querying permission does not throw errors; it returns the current state as a string.
Final Answer:
"prompt" -> Option D
Quick Check:
Unrequested permission state = prompt [OK]
Hint: Unasked permissions show 'prompt' state [OK]
A. Missing await before Deno.permissions.request call.
B. Using wrong permission name "read" instead of "readFile".
C. Cannot check permission.state directly; must use a callback.
D. request() does not return a permission status object.
Solution
Step 1: Check async usage of request()
The request() method returns a Promise, so it must be awaited to get the permission status object.
Step 2: Identify missing await impact
Without await, permission is a Promise, so accessing permission.state is invalid and causes errors.
Final Answer:
Missing await before Deno.permissions.request call. -> Option A
Quick Check:
Always await request() for permission object [OK]
Hint: Always await async permission requests [OK]
Common Mistakes:
Forgetting await on async calls
Confusing permission names
Trying to use callbacks instead of promises
5. You want to write a Deno script that reads a file only if the user grants permission at runtime. Which approach correctly handles permission requests and reading the file?
hard
A. const status = Deno.permissions.query({ name: "read", path: "file.txt" });
if (status.state === "granted") {
const data = Deno.readTextFileSync("file.txt");
console.log(data);
}
B. const status = await Deno.permissions.request({ name: "read", path: "file.txt" });
if (status.state === "granted") {
const data = await Deno.readTextFile("file.txt");
console.log(data);
} else {
console.log("Permission denied.");
}
C. await Deno.readTextFile("file.txt");
console.log("File read without permission check.");
D. Deno.permissions.request({ name: "read" });
const data = await Deno.readTextFile("file.txt");
console.log(data);
Solution
Step 1: Request permission with await and check result
const status = await Deno.permissions.request({ name: "read", path: "file.txt" });
if (status.state === "granted") {
const data = await Deno.readTextFile("file.txt");
console.log(data);
} else {
console.log("Permission denied.");
} correctly awaits the permission request and checks if the state is "granted" before proceeding.
Step 2: Read file only if permission granted
It reads the file asynchronously only after permission is granted, otherwise it informs the user permission was denied.
Step 3: Identify errors in other options
const status = Deno.permissions.query({ name: "read", path: "file.txt" });
if (status.state === "granted") {
const data = Deno.readTextFileSync("file.txt");
console.log(data);
} uses query() which does not request permission; await Deno.readTextFile("file.txt");
console.log("File read without permission check."); reads without permission check; Deno.permissions.request({ name: "read" });
const data = await Deno.readTextFile("file.txt");
console.log(data); does not await request() and reads file regardless.
Final Answer:
const status = await Deno.permissions.request({ name: "read", path: "file.txt" });
if (status.state === "granted") {
const data = await Deno.readTextFile("file.txt");
console.log(data);
} else {
console.log("Permission denied.");
} -> Option B
Quick Check:
Request permission, await result, then read file [OK]
Hint: Always await request() and check state before file access [OK]
Common Mistakes:
Using query() instead of request() to ask permission
Not awaiting async permission requests
Reading files without permission check
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.