Signals are messages from the operating system to your program.
Always dispose signal listeners to avoid resource leaks.
Use Deno.run() to start and manage child processes.
Summary
Signal handling lets your program respond to system events like Ctrl+C.
Process management helps you run and control other programs from your code.
Always clean up signal listeners and child processes to keep your program healthy.
Practice
(1/5)
1. What is the main purpose of signal handling in a Deno program?
easy
A. To respond to system events like Ctrl+C or termination requests
B. To speed up the execution of asynchronous code
C. To automatically restart the program after an error
D. To manage memory allocation for the program
Solution
Step 1: Understand what signals are
Signals are system messages sent to a program, such as when a user presses Ctrl+C.
Step 2: Identify signal handling purpose
Signal handling lets the program catch these signals and respond, for example, to clean up resources before exiting.
Final Answer:
To respond to system events like Ctrl+C or termination requests -> Option A
Quick Check:
Signal handling = respond to system events [OK]
Hint: Signals catch system events like Ctrl+C to handle them [OK]
Common Mistakes:
Confusing signal handling with error handling
Thinking signal handling speeds up code
Assuming signals manage memory automatically
2. Which of the following is the correct way to listen for a SIGINT signal in Deno?
easy
A. Deno.listenSignal('SIGINT', () => console.log('Interrupted'))
B. Deno.signal(Deno.Signal.SIGINT).then(() => console.log('Interrupted'))
C. Deno.on('SIGINT', () => console.log('Interrupted'))
D. for await (const _ of Deno.signal(Deno.Signal.SIGINT)) { console.log('Interrupted') }
Solution
Step 1: Recall Deno's signal API usage
Deno.signal returns an async iterable for the given signal.
Step 2: Identify correct syntax to listen continuously
Using a for-await-of loop over Deno.signal(Deno.Signal.SIGINT) listens repeatedly for SIGINT events.
Final Answer:
for await (const _ of Deno.signal(Deno.Signal.SIGINT)) { console.log('Interrupted') } -> Option D
Quick Check:
Use for-await-of with Deno.signal for signals [OK]
Hint: Use for-await-of loop with Deno.signal to handle signals [OK]
Common Mistakes:
Using .then() which only handles one signal once
Trying to use event emitter style which Deno does not support
Using non-existent Deno.listenSignal method
3. What will the following Deno code output when you press Ctrl+C once?
const sig = Deno.signal(Deno.Signal.SIGINT);
for await (const _ of sig) {
console.log('Signal received');
sig.dispose();
}
medium
A. Signal received (printed once), then program stops listening for SIGINT
B. SyntaxError because dispose() cannot be called inside loop
C. Signal received printed repeatedly on every Ctrl+C press
D. Signal received (printed once), then program continues running
Solution
Step 1: Understand the for-await-of loop over signal
The loop waits for SIGINT signals and runs the body each time one occurs.
Step 2: Effect of calling sig.dispose()
Calling dispose() stops the signal listener, so after the first Ctrl+C, the loop ends and no more signals are handled.
Final Answer:
Signal received (printed once), then program stops listening for SIGINT -> Option A
Quick Check:
dispose() stops signal listening after first event [OK]
Hint: dispose() stops signal listening; loop ends after first signal [OK]
Common Mistakes:
Thinking dispose() only pauses temporarily
Assuming the loop continues printing on every Ctrl+C
Believing dispose() causes syntax errors
4. Identify the error in this Deno code snippet that tries to spawn a child process and handle its exit:
const p = Deno.run({ cmd: ["ls", "-l"] });
p.status().then(status => {
console.log(`Process exited with code ${status.code}`);
});
p.close();
medium
A. Deno.run does not exist; should use Deno.spawn instead
B. Missing await before p.status() causes unhandled promise
C. Calling p.close() before the process finishes causes resource leaks
D. The cmd array should be a string, not an array
Solution
Step 1: Understand process lifecycle in Deno
Deno.run starts a child process and returns a process handle.
Step 2: Analyze order of close() call
Calling p.close() immediately releases resources before the process finishes, which can cause issues.
Final Answer:
Calling p.close() before the process finishes causes resource leaks -> Option C
Quick Check:
Close process handle only after process ends [OK]
Hint: Call close() only after process completes to avoid leaks [OK]
Common Mistakes:
Calling close() too early before process ends
Confusing Deno.run with Deno.spawn (spawn is newer API)
Thinking cmd should be a string instead of array
5. You want to write a Deno script that runs a child process and gracefully handles Ctrl+C to kill the child before exiting. Which approach correctly combines signal handling and process management?
hard
A. Call child.kill() immediately after spawning, then listen for SIGINT to exit
B. Use for-await-of on Deno.signal(Deno.Signal.SIGINT) to catch Ctrl+C, then call child.kill() and await child.status() before exiting
C. Use Deno.run with cmd and ignore signals; rely on OS to kill child on exit
D. Spawn child process with Deno.spawn and never call kill(), just exit on SIGINT
Solution
Step 1: Understand graceful shutdown
On Ctrl+C, the program should catch SIGINT and clean up child processes before exiting.
Step 2: Combine signal listening and child process control
Using for-await-of on Deno.signal(Deno.Signal.SIGINT) allows catching Ctrl+C; then calling child.kill() stops the child, and awaiting child.status() ensures it ends cleanly.
Final Answer:
Use for-await-of on Deno.signal(Deno.Signal.SIGINT) to catch Ctrl+C, then call child.kill() and await child.status() before exiting -> Option B
Quick Check:
Signal handling + kill child + await status = graceful exit [OK]
Hint: Catch SIGINT, kill child, await status for clean exit [OK]
Common Mistakes:
Killing child before signal received
Ignoring signals and leaving child running
Not awaiting child.status() causing premature exit
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.