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