79305876

Date: 2024-12-24 14:59:44
Score: 0.5
Natty:
Report link

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;
}
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Finley Baker