blob: 8c8dea88054963396cd89d11d02ec59e5e3d7160 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
## Handle OS Signals
> This program makes use of an unstable Deno feature. Learn more about
> [unstable features](../runtime/stability.md).
[API Reference](https://deno.land/typedoc/index.html#signal)
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.
|