diff --git a/index.js b/index.js index 6c64f0bb7b..06f39697d6 100644 --- a/index.js +++ b/index.js @@ -216,11 +216,15 @@ module.exports = (cmd, args, opts) => { let timeoutId = null; let timedOut = false; - const cleanupTimeout = () => { + const cleanup = () => { if (timeoutId) { clearTimeout(timeoutId); timeoutId = null; } + + if (removeExitHandler) { + removeExitHandler(); + } }; if (parsed.opts.timeout > 0) { @@ -233,18 +237,18 @@ module.exports = (cmd, args, opts) => { const processDone = new Promise(resolve => { spawned.on('exit', (code, signal) => { - cleanupTimeout(); + cleanup(); resolve({code, signal}); }); spawned.on('error', err => { - cleanupTimeout(); + cleanup(); resolve({error: err}); }); if (spawned.stdin) { spawned.stdin.on('error', err => { - cleanupTimeout(); + cleanup(); resolve({error: err}); }); } @@ -269,10 +273,6 @@ module.exports = (cmd, args, opts) => { result.stdout = arr[1]; result.stderr = arr[2]; - if (removeExitHandler) { - removeExitHandler(); - } - if (result.error || result.code !== 0 || result.signal !== null) { const err = makeError(result, { joinedCmd,