diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2021-07-20 16:25:36 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-20 16:25:36 +0200 |
commit | d744c0c6d9a557bbaa2a23571ffb3acabf19c35a (patch) | |
tree | 6f7fb8a71b786e79c48f4b2c11a5a9ca988717e8 /docs/examples/os_signals.md | |
parent | 9b9becf1ae256b645e37a7eecf3441f3ae4b8ea5 (diff) |
chore: move docs to separate repository
Diffstat (limited to 'docs/examples/os_signals.md')
-rw-r--r-- | docs/examples/os_signals.md | 83 |
1 files changed, 0 insertions, 83 deletions
diff --git a/docs/examples/os_signals.md b/docs/examples/os_signals.md deleted file mode 100644 index e49c3eb76..000000000 --- a/docs/examples/os_signals.md +++ /dev/null @@ -1,83 +0,0 @@ -# Handle OS Signals - -> This program makes use of an unstable Deno feature. Learn more about -> [unstable features](../runtime/stability.md). - -## Concepts - -- Use the `--unstable` flag to access new or unstable features in Deno. -- [Deno.signal](https://doc.deno.land/builtin/unstable#Deno.signal) can be used - to capture and monitor OS signals. -- Use the `dispose()` function of the Deno.signal - [SignalStream](https://doc.deno.land/builtin/unstable#Deno.SignalStream) to - stop watching the signal. - -## Async iterator example - -You can use `Deno.signal()` function for handling OS signals: - -```ts -/** - * async-iterator-signal.ts - */ -console.log("Press Ctrl-C to trigger a SIGINT signal"); -for await (const _ of Deno.signal(Deno.Signal.SIGINT)) { - console.log("interrupted!"); - Deno.exit(); -} -``` - -Run with: - -```shell -deno run --unstable async-iterator-signal.ts -``` - -## Promise based example - -`Deno.signal()` also works as a promise: - -```ts -/** - * promise-signal.ts - */ -console.log("Press Ctrl-C to trigger a SIGINT signal"); -await Deno.signal(Deno.Signal.SIGINT); -console.log("interrupted!"); -Deno.exit(); -``` - -Run with: - -```shell -deno run --unstable promise-signal.ts -``` - -## Stop watching signals - -If you want to stop watching the signal, you can use `dispose()` method of the -signal object: - -```ts -/** - * dispose-signal.ts - */ -const sig = Deno.signal(Deno.Signal.SIGINT); -setTimeout(() => { - sig.dispose(); - console.log("No longer watching SIGINT signal"); -}, 5000); - -console.log("Watching SIGINT signals"); -for await (const _ of sig) { - console.log("interrupted"); -} -``` - -Run with: - -```shell -deno run --unstable dispose-signal.ts -``` - -The above for-await loop exits after 5 seconds when `sig.dispose()` is called. |