From 2ef8382a6a7b34783cc1bd7a4ab7db163cff1bd0 Mon Sep 17 00:00:00 2001 From: sw-yx Date: Wed, 13 Feb 2019 01:30:24 -1000 Subject: [PATCH] rewrite timeout feature for node <10 --- lib/serve.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/serve.js b/lib/serve.js index 5f495126..d40b9e0e 100644 --- a/lib/serve.js +++ b/lib/serve.js @@ -136,19 +136,26 @@ function createHandler(dir, static, timeout) { callback ); - var invocationTimeoutRef = null - + var invocationTimeoutRef = null; + Promise.race([ promiseCallback(promise, callback), new Promise(function(resolve) { invocationTimeoutRef = setTimeout(function() { - handleInvocationTimeout(response, timeout) - resolve() - }, timeout * 1000) + handleInvocationTimeout(response, timeout); + resolve(); + }, timeout * 1000); }) - ]).finally(() => { - clearTimeout(invocationTimeoutRef) - }) + ]).then( + result => { + clearTimeout(invocationTimeoutRef); + return result; // not used, but writing this to avoid future footguns + }, + err => { + clearTimeout(invocationTimeoutRef); + throw err; + } + ); }; }