From f24e7c7237de2c06f19903eecf3f04f0785c29d1 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Wed, 20 Mar 2019 23:41:18 +0700 Subject: [PATCH] Clean up #189 --- readme.md | 25 +++++++++++++------------ test.js | 50 +++++++++++++++++++++++++------------------------- 2 files changed, 38 insertions(+), 37 deletions(-) diff --git a/readme.md b/readme.md index 2ffaecbca..53eb19ce5 100644 --- a/readme.md +++ b/readme.md @@ -52,16 +52,6 @@ const execa = require('execa'); const {stdout} = await execa.shell('echo unicorns'); //=> 'unicorns' - // Cancelling a spawned process - const subprocess = execa('node'); - setTimeout(() => { spawned.cancel() }, 1000); - try { - await subprocess; - } catch (error) { - console.log(subprocess.killed); // true - console.log(error.isCanceled); // true - } - // Catching an error try { await execa.shell('exit 3'); @@ -83,6 +73,18 @@ const execa = require('execa'); } */ } + + // Cancelling a spawned process + 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 a sync method @@ -119,8 +121,7 @@ Returns a [`child_process` instance](https://nodejs.org/api/child_process.html#c It exposes an additional `.all` stream, with `stdout` and `stderr` interleaved. -It can be canceled with `.cancel` method which throws an error with `error.isCanceled` equal to `true`, provided that the process gets canceled. -Process would not get canceled if it has already exited. +The spawned process can be canceled with the `.cancel()` method on the promise, which causes the promise to reject an error with a `.isCanceled = true` property, provided the process gets canceled. The process will not be canceled if it has already exited. The promise result is an `Object` with `stdout`, `stderr` and `all` properties. diff --git a/test.js b/test.js index 38f66e6de..6dfb7430b 100644 --- a/test.js +++ b/test.js @@ -587,10 +587,10 @@ if (Promise.prototype.finally) { }); } -test('cancel method kills the spawned process', t => { - const spawned = execa('node'); - spawned.cancel(); - t.true(spawned.killed); +test('cancel method kills the subprocess', t => { + const subprocess = execa('node'); + subprocess.cancel(); + t.true(subprocess.killed); }); test('result.isCanceled is false when spawned.cancel isn\'t called', async t => { @@ -599,44 +599,44 @@ test('result.isCanceled is false when spawned.cancel isn\'t called', async t => }); test('calling cancel method throws an error with message "Command was canceled"', async t => { - const spawned = execa('noop'); - spawned.cancel(); - await t.throwsAsync(spawned, {message: /Command was canceled/}); + const subprocess = execa('noop'); + subprocess.cancel(); + await t.throwsAsync(subprocess, {message: /Command was canceled/}); }); test('error.isCanceled is true when cancel method is used', async t => { - const spawned = execa('noop'); - spawned.cancel(); - const error = await t.throwsAsync(spawned); + const subprocess = execa('noop'); + subprocess.cancel(); + const error = await t.throwsAsync(subprocess); t.true(error.isCanceled); }); test('error.isCanceled is false when kill method is used', async t => { - const spawned = execa('noop'); - spawned.kill(); - const error = await t.throwsAsync(spawned); + const subprocess = execa('noop'); + subprocess.kill(); + const error = await t.throwsAsync(subprocess); t.false(error.isCanceled); }); test('calling cancel method twice should show the same behaviour as calling it once', async t => { - const spawned = execa('noop'); - spawned.cancel(); - spawned.cancel(); - const error = await t.throwsAsync(spawned); + const subprocess = execa('noop'); + subprocess.cancel(); + subprocess.cancel(); + const error = await t.throwsAsync(subprocess); t.true(error.isCanceled); - t.true(spawned.killed); + t.true(subprocess.killed); }); -test('calling cancel method on a successfuly completed process does not make result.cancel true', async t => { - const spawned = execa('noop'); - const result = await spawned; - spawned.cancel(); +test('calling cancel method on a successfuly completed process does not make result.isCanceled true', async t => { + const subprocess = execa('noop'); + const result = await subprocess; + subprocess.cancel(); t.false(result.isCanceled); }); test('calling cancel method on a process which has been killed does not make error.isCanceled true', async t => { - const spawned = execa('noop'); - spawned.kill(); - const error = await t.throwsAsync(spawned); + const subprocess = execa('noop'); + subprocess.kill(); + const error = await t.throwsAsync(subprocess); t.false(error.isCanceled); });