From 20cb4679507156b2a33389a2375d76233af0a8cd Mon Sep 17 00:00:00 2001 From: Alex Anderson Date: Tue, 12 Feb 2019 19:16:10 -0700 Subject: [PATCH] Update serve.js to clear the timeout if a function invocation is a success within the timeout. --- lib/serve.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/serve.js b/lib/serve.js index bafc543d..5f495126 100644 --- a/lib/serve.js +++ b/lib/serve.js @@ -135,12 +135,20 @@ function createHandler(dir, static, timeout) { { clientContext: buildClientContext(request.headers) || {} }, callback ); + + var invocationTimeoutRef = null + Promise.race([ promiseCallback(promise, callback), - setTimeout(function() { - handleInvocationTimeout(response, timeout) - }, timeout * 1000) - ]) + new Promise(function(resolve) { + invocationTimeoutRef = setTimeout(function() { + handleInvocationTimeout(response, timeout) + resolve() + }, timeout * 1000) + }) + ]).finally(() => { + clearTimeout(invocationTimeoutRef) + }) }; }