There is actually a workaround for this behavior though it's a bit obtuse. I think the issue is that the signal comes in while the prompt
is waiting for user input. We can overcome this obstacle by using a promise and setTimeout
:
let sigint = false;
Deno.addSignalListener("SIGINT", () => {
sigint = true;
});
// Add a timeout to prevent process exiting immediately.
while (true) {
const val = prompt("Prompt: ");
const printHandlerPromise = new Promise((resolve, reject) => {
console.log(val);
setTimeout(() => {
if (sigint) {
console.log('sigint!');
sigint = false;
}
resolve(null);
}, 0);
});
await printHandlerPromise;
}