Bird
Raised Fist0
Denoframework~10 mins

Permission prompts at runtime in Deno - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Permission prompts at runtime
Start Program
↓
Request Permission
↓
User Prompt Displayed
↓
Permission Granted
↓
Run Protected Code
↓
End
The program starts and requests permission. The user sees a prompt and chooses Yes or No. If Yes, the program runs protected code; if No, it handles denial or exits.
Execution Sample
Deno
const status = await Deno.permissions.request({ name: "read" });
if (status.state === "granted") {
  console.log("Permission granted");
} else {
  console.log("Permission denied");
}
This code asks for read permission at runtime and logs if permission was granted or denied.
Execution Table
StepActionPermission State BeforeUser ChoicePermission State AfterOutput
1Request read permissionpromptUser sees promptpromptNo output yet
2User selects YespromptYesgrantedNo output yet
3Check permission stategranted-grantedPermission granted
4Program runs protected codegranted-grantedProtected code executes
5End programgranted-grantedProgram ends
6Alternative: User selects NopromptNodeniedNo output yet
7Check permission statedenied-deniedPermission denied
8Handle denial or exitdenied-deniedProgram exits or handles denial
💡 Execution stops after permission is granted and protected code runs, or after denial is handled.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
status.stateundefinedpromptgranted or deniedgranted or deniedgranted or denied
Key Moments - 3 Insights
Why does the program wait at the permission request step?
Because the program pauses to show the prompt and wait for the user to choose Yes or No, as shown in execution_table step 1 and 2.
What happens if the user denies permission?
The permission state becomes 'denied' and the program runs the denial handling code, as seen in execution_table steps 6 to 8.
Can the program run protected code without permission?
No, the program only runs protected code if permission is 'granted', as shown in execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the permission state after the user selects Yes?
Aprompt
Bdenied
Cgranted
Dundefined
💡 Hint
Check execution_table row 2 under 'Permission State After'
At which step does the program output 'Permission denied'?
AStep 3
BStep 7
CStep 4
DStep 1
💡 Hint
Look at execution_table row 7 under 'Output'
If the user always selects No, what will the program do?
AExit or handle denial
BRequest permission again automatically
CRun protected code
DIgnore permission and continue
💡 Hint
See execution_table rows 6 to 8 for denial path
Concept Snapshot
Deno permission prompts ask user at runtime.
Use Deno.permissions.request({name: "permission"}).
Program pauses until user responds.
If granted, run protected code.
If denied, handle or exit.
Always check permission state before sensitive actions.
Full Transcript
In Deno, when a program needs special access like reading files, it asks the user for permission at runtime. The program pauses and shows a prompt. The user can allow or deny. If allowed, the program continues and runs the code needing permission. If denied, the program handles this by exiting or showing a message. This process ensures safety by letting users control what the program can do. The key is to check the permission state after the prompt before running sensitive code.

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

  1. 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.
  2. Step 2: Compare with other permission methods

    Unlike request() which asks for permission, query() only checks the status without changing it.
  3. Final Answer:

    It checks if a specific permission is already granted or denied. -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    await Deno.permissions.request({ name: "read", path: "data.txt" }); -> Option C
  4. 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

  1. 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.
  2. Step 2: Confirm no error thrown on query

    Querying permission does not throw errors; it returns the current state as a string.
  3. Final Answer:

    "prompt" -> Option D
  4. Quick Check:

    Unrequested permission state = prompt [OK]
Hint: Unasked permissions show 'prompt' state [OK]
Common Mistakes:
  • Assuming default is granted or denied
  • Expecting an error on query
  • Confusing query() with request() behavior
4. What is wrong with this Deno code snippet?
const permission = Deno.permissions.request({ name: "read" });
if (permission.state === "granted") {
  console.log("Access allowed");
}
medium
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

  1. Step 1: Check async usage of request()

    The request() method returns a Promise, so it must be awaited to get the permission status object.
  2. Step 2: Identify missing await impact

    Without await, permission is a Promise, so accessing permission.state is invalid and causes errors.
  3. Final Answer:

    Missing await before Deno.permissions.request call. -> Option A
  4. 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

  1. 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.
  2. 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.
  3. 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.
  4. 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
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard