From c2787e5f6ba6806af8f4168af8e6899b37c641c8 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Thu, 20 Jun 2019 04:49:32 -0700 Subject: [PATCH] Separate `cleanup` into its own function (#308) --- index.js | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 6ee2571b0b..b2f9c9a603 100644 --- a/index.js +++ b/index.js @@ -345,6 +345,16 @@ const setExitHandler = (spawned, {cleanup, detached}, context) => { }); }; +const cleanup = ({timeoutId, removeExitHandler}) => { + if (timeoutId !== undefined) { + clearTimeout(timeoutId); + } + + if (removeExitHandler !== undefined) { + removeExitHandler(); + } +}; + const execa = (file, args, options) => { const parsed = handleArgs(file, args, options); const command = joinCommand(file, args); @@ -370,22 +380,13 @@ const execa = (file, args, options) => { const context = {timedOut: false}; let isCanceled = false; - const cleanup = () => { - if (context.timeoutId !== undefined) { - clearTimeout(context.timeoutId); - context.timeoutId = undefined; - } - - if (context.removeExitHandler) { - context.removeExitHandler(); - } - }; - setExitHandler(spawned, parsed.options, context); setupTimeout(spawned, parsed.options, context); // TODO: Use native "finally" syntax when targeting Node.js 10 - const processDone = pFinally(handleSpawned(spawned, context), cleanup); + const processDone = pFinally(handleSpawned(spawned, context), () => { + cleanup(context); + }); const handlePromise = async () => { const [result, stdout, stderr, all] = await getPromiseResult(spawned, parsed.options, processDone);