Skip to content

Commit

Permalink
rewrite timeout feature for node <10
Browse files Browse the repository at this point in the history
  • Loading branch information
sw-yx committed Feb 13, 2019
1 parent c562d7c commit 2ef8382
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions lib/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
);
};
}

Expand Down

0 comments on commit 2ef8382

Please sign in to comment.