Clean up #189
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sindre Sorhus · committed by Sindre Sorhus · parent 79356cc · Browse files · 2 files·+38 added−37 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+38 added−37 removed
- other:+38 added−37 removed
Provenance
- Commit
f24e7c7- Tree
27ef09b- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
79356cc
2 files changed+38 added−37 removed
modified:readme.md+13 added-12 removed
50 unmodified lines51 // Run a shell command52 const {stdout} = await execa.shell('echo unicorns');53 //=> 'unicorns'54 55- // Cancelling a spawned process56- const subprocess = execa('node');57- setTimeout(() => { spawned.cancel() }, 1000);58- try {59- await subprocess;60- } catch (error) {61- console.log(subprocess.killed); // true62- console.log(error.isCanceled); // true63- }64-55 // Catching an error56 try {57 await execa.shell('exit 3');58 } catch (error) {13 unmodified lines72 killed: false73 }74 */75 }76+77+ // Cancelling a spawned process78+ const subprocess = execa('node');79+ setTimeout(() => {80+ subprocess.cancel();81+ }, 1000);82+ try {83+ await subprocess;84+ } catch (error) {85+ console.log(subprocess.killed); // true86+ console.log(error.isCanceled); // true87+ }88 })();89 90 // Catching an error with a sync method91 try {28 unmodified lines120 Returns a [`child_process` instance](https://nodejs.org/api/child_process.html#child_process_class_childprocess) which is enhanced to be a `Promise`.121 122 It exposes an additional `.all` stream, with `stdout` and `stderr` interleaved.123 122-It can be canceled with `.cancel` method which throws an error with `error.isCanceled` equal to `true`, provided that the process gets canceled.123-Process would not get canceled if it has already exited.124+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.125 126 The promise result is an `Object` with `stdout`, `stderr` and `all` properties.127 128 ### execa.stdout(file, [arguments], [options])225 unmodified linesmodified:test.js+25 added-25 removed
585 unmodified lines586 t.is(result.message, 'called');587 });588 }589 590-test('cancel method kills the spawned process', t => {591- const spawned = execa('node');592- spawned.cancel();593- t.true(spawned.killed);590+test('cancel method kills the subprocess', t => {591+ const subprocess = execa('node');592+ subprocess.cancel();593+ t.true(subprocess.killed);594 });595 596 test('result.isCanceled is false when spawned.cancel isn\'t called', async t => {597 const result = await execa('noop');598 t.false(result.isCanceled);599 });600 601 test('calling cancel method throws an error with message "Command was canceled"', async t => {602- const spawned = execa('noop');603- spawned.cancel();604- await t.throwsAsync(spawned, {message: /Command was canceled/});602+ const subprocess = execa('noop');603+ subprocess.cancel();604+ await t.throwsAsync(subprocess, {message: /Command was canceled/});605 });606 607 test('error.isCanceled is true when cancel method is used', async t => {608- const spawned = execa('noop');609- spawned.cancel();610- const error = await t.throwsAsync(spawned);608+ const subprocess = execa('noop');609+ subprocess.cancel();610+ const error = await t.throwsAsync(subprocess);611 t.true(error.isCanceled);612 });613 614 test('error.isCanceled is false when kill method is used', async t => {615- const spawned = execa('noop');616- spawned.kill();617- const error = await t.throwsAsync(spawned);615+ const subprocess = execa('noop');616+ subprocess.kill();617+ const error = await t.throwsAsync(subprocess);618 t.false(error.isCanceled);619 });620 621 test('calling cancel method twice should show the same behaviour as calling it once', async t => {622- const spawned = execa('noop');623- spawned.cancel();624- spawned.cancel();625- const error = await t.throwsAsync(spawned);622+ const subprocess = execa('noop');623+ subprocess.cancel();624+ subprocess.cancel();625+ const error = await t.throwsAsync(subprocess);626 t.true(error.isCanceled);627- t.true(spawned.killed);627+ t.true(subprocess.killed);628 });629 630-test('calling cancel method on a successfuly completed process does not make result.cancel true', async t => {631- const spawned = execa('noop');632- const result = await spawned;633- spawned.cancel();630+test('calling cancel method on a successfuly completed process does not make result.isCanceled true', async t => {631+ const subprocess = execa('noop');632+ const result = await subprocess;633+ subprocess.cancel();634 t.false(result.isCanceled);635 });636 637 test('calling cancel method on a process which has been killed does not make error.isCanceled true', async t => {638- const spawned = execa('noop');639- spawned.kill();640- const error = await t.throwsAsync(spawned);638+ const subprocess = execa('noop');639+ subprocess.kill();640+ const error = await t.throwsAsync(subprocess);641 t.false(error.isCanceled);642 });