import { Deno } from "https://deno.land/x/deno@v1.0.0/cli/js/lib.deno.unstable.d.ts";
const { signal } = Deno;
UNSTABLE: new API, yet to be vetted.
Returns the stream of the given signal number. You can use it as an async iterator.
for await (const _ of Deno.signal(Deno.Signal.SIGTERM)) {
console.log("got SIGTERM!");
}
You can also use it as a promise. In this case you can only receive the first one.
await Deno.signal(Deno.Signal.SIGTERM);
console.log("SIGTERM received!")
If you want to stop receiving the signals, you can use .dispose()
method
of the signal stream object.
const sig = Deno.signal(Deno.Signal.SIGTERM);
setTimeout(() => { sig.dispose(); }, 5000);
for await (const _ of sig) {
console.log("SIGTERM!")
}
The above for-await loop exits after 5 seconds when sig.dispose()
is
called.
NOTE: This functionality is not yet implemented on Windows.