From 44f436fa15944523fe3f1646db836478418eb53f Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Fri, 24 Jan 2020 22:15:31 +0900 Subject: [PATCH] Add signal handlers (denoland/deno#3757) --- manual.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/manual.md b/manual.md index ece5a755b9f8..204701566f17 100644 --- a/manual.md +++ b/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