From 2956ea147902854ba00cc7547e19277bf83c1473 Mon Sep 17 00:00:00 2001 From: Dan Levy <397632+justsml@users.noreply.github.com> Date: Sat, 2 Nov 2019 21:08:08 -0600 Subject: [PATCH] Improve the readme usage examples (#385) * Cleanup & label examples * Update readme.md * Update readme.md --- readme.md | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/readme.md b/readme.md index 18f5099318..9c826d5e9b 100644 --- a/readme.md +++ b/readme.md @@ -41,16 +41,20 @@ const execa = require('execa'); })(); ``` -Additional examples: +### Pipe the child process stdout to the parent ```js const execa = require('execa'); -(async () => { - // Pipe the child process stdout to the current stdout - execa('echo', ['unicorns']).stdout.pipe(process.stdout); +execa('echo', ['unicorns']).stdout.pipe(process.stdout); +``` +### Handling Errors +```js +const execa = require('execa'); + +(async () => { // Catching an error try { await execa('unknown', ['command']); @@ -77,20 +81,33 @@ const execa = require('execa'); */ } - // Cancelling a spawned process +})(); +``` + +### Cancelling a spawned process + +```js +const execa = require('execa'); + +(async () => { const subprocess = execa('node'); + setTimeout(() => { subprocess.cancel(); }, 1000); + try { await subprocess; } catch (error) { console.log(subprocess.killed); // true console.log(error.isCanceled); // true } -})(); +})() +``` + +### Catching an error with the sync method -// Catching an error with a sync method +```js try { execa.sync('unknown', ['command']); } catch (error) { @@ -115,9 +132,15 @@ try { } */ } +``` + +### Kill a process + +Using SIGTERM, and after 2 seconds, kill it with SIGKILL. -// Kill a process with SIGTERM, and after 2 seconds, kill it with SIGKILL +```js const subprocess = execa('node'); + setTimeout(() => { subprocess.kill('SIGTERM', { forceKillAfterTimeout: 2000 @@ -125,6 +148,7 @@ setTimeout(() => { }, 1000); ``` + ## API ### execa(file, arguments, [options])