Permission prompts let your program ask the user for access to files, network, or other resources while it runs. This keeps the computer safe by controlling what the program can do.
Permission prompts at runtime in Deno
const status = await Deno.permissions.query({ name: "read", path: "/path/to/file" }); if (status.state === "granted") { // permission granted, safe to read file } else { // permission denied or prompt needed }
You use Deno.permissions.query() to check if permission is already granted.
Permission names include read, write, net, env, and run.
const readStatus = await Deno.permissions.query({ name: "read" }); console.log(readStatus.state);
const netStatus = await Deno.permissions.request({ name: "net", host: "example.com" }); console.log(netStatus.state);
const writeStatus = await Deno.permissions.request({ name: "write", path: "/tmp/data.txt" }); if (writeStatus.state === "granted") { // safe to write to /tmp/data.txt }
This program asks the user for permission to read a file named example.txt. If allowed, it reads and prints the file content. Otherwise, it shows a denial message.
async function checkAndReadFile() { const permission = await Deno.permissions.request({ name: "read", path: "./example.txt" }); if (permission.state === "granted") { const data = await Deno.readTextFile("./example.txt"); console.log("File content:", data); } else { console.log("Permission to read file was denied."); } } checkAndReadFile();
Permissions can be granted, denied, or prompt (waiting for user decision).
Always handle the case when permission is denied to avoid program crashes.
Request only the permissions you need, when you need them, to keep users safe and trustful.
Permission prompts help control what your program can access at runtime.
Use Deno.permissions.query() to check and Deno.permissions.request() to ask for permissions.
Handle permission results carefully to keep your program safe and user-friendly.
Practice
Deno.permissions.query() do in a Deno program?Solution
Step 1: Understand the purpose of
This method is used to check the current status of a permission, whether it is granted, denied, or prompt.Deno.permissions.query()Step 2: Compare with other permission methods
Unlikerequest()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 AQuick Check:
Permission check = A [OK]
- Confusing query() with request()
- Thinking query() grants permissions
- Assuming query() revokes permissions
data.txt in Deno?Solution
Step 1: Identify correct method and parameters
Therequest()method takes an object with anameproperty for permission type and optionalpathfor file-specific permission.Step 2: Check syntax correctness
await Deno.permissions.request({ name: "read", path: "data.txt" }); uses correct async call with an object containingname: "read"andpath: "data.txt". Other options misuse parameters or use query instead of request.Final Answer:
await Deno.permissions.request({ name: "read", path: "data.txt" }); -> Option CQuick Check:
Correct request syntax = B [OK]
- Passing parameters as separate arguments
- Using query() instead of request()
- Wrong property names like permission or file
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?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 DQuick Check:
Unrequested permission state = prompt [OK]
- Assuming default is granted or denied
- Expecting an error on query
- Confusing query() with request() behavior
const permission = Deno.permissions.request({ name: "read" });
if (permission.state === "granted") {
console.log("Access allowed");
}Solution
Step 1: Check async usage of request()
Therequest()method returns a Promise, so it must be awaited to get the permission status object.Step 2: Identify missing await impact
Without await,permissionis a Promise, so accessingpermission.stateis invalid and causes errors.Final Answer:
Missing await before Deno.permissions.request call. -> Option AQuick Check:
Always await request() for permission object [OK]
- Forgetting await on async calls
- Confusing permission names
- Trying to use callbacks instead of promises
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 BQuick Check:
Request permission, await result, then read file [OK]
- Using query() instead of request() to ask permission
- Not awaiting async permission requests
- Reading files without permission check
Ready to prove it?
Pass the test and this pattern moves unknown
