Skip to content

Latest commit

 

History

History
35 lines (25 loc) · 817 Bytes

os_signals.md

File metadata and controls

35 lines (25 loc) · 817 Bytes

Handle OS Signals

This program makes use of an unstable Deno feature. Learn more about unstable features.

API Reference

You can use Deno.signal() function for handling OS signals.

for await (const _ of Deno.signal(Deno.Signal.SIGINT)) {
  console.log("interrupted!");
}

Deno.signal() also works as a promise.

await Deno.signal(Deno.Singal.SIGINT);
console.log("interrupted!");

If you want to stop watching the signal, you can use dispose() method of the signal object.

const sig = Deno.signal(Deno.Signal.SIGINT);
setTimeout(() => { sig.dispose(); }, 5000);

for await (const _ of sig) {
  console.log("interrupted");
}

The above for-await loop exits after 5 seconds when sig.dispose() is called.