summaryrefslogtreecommitdiff
path: root/std/manual.md
diff options
context:
space:
mode:
authorYoshiya Hinosawa <stibium121@gmail.com>2020-01-24 22:15:31 +0900
committerRyan Dahl <ry@tinyclouds.org>2020-01-24 08:15:31 -0500
commitbc89f04cbf5e7a8480331eea77e57a100c8179d7 (patch)
treecb5b466f1169872f1e47d68926c27fbbd1509c3f /std/manual.md
parenta6a7253df9129b51255d45bf996d9a335c809ace (diff)
Add signal handlers (#3757)
Diffstat (limited to 'std/manual.md')
-rw-r--r--std/manual.md33
1 files changed, 33 insertions, 0 deletions
diff --git a/std/manual.md b/std/manual.md
index ece5a755b..204701566 100644
--- a/std/manual.md
+++ b/std/manual.md
@@ -428,6 +428,39 @@ Uncaught NotFound: No such file or directory (os error 2)
at handleAsyncMsgFromRust (deno/js/dispatch.ts:27:17)
```
+### Handle OS Signals
+
+[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.
+
### Linking to third party code
In the above examples, we saw that Deno could execute scripts from URLs. Like