diff options
author | Marvin Hagemeister <marvin@deno.com> | 2023-06-10 12:17:56 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-10 12:17:56 +0200 |
commit | f3326eebd6af2aaca1543e8cb543a7b16762bc96 (patch) | |
tree | 77468152f3d0168c57ef44fe4e1ee77e3cc0be4a /ext/http/00_serve.js | |
parent | 848cda619ec5de6e8bd03783c35ef726824be01c (diff) |
perf(serve): hoist promise error callback (#19456)
Diffstat (limited to 'ext/http/00_serve.js')
-rw-r--r-- | ext/http/00_serve.js | 27 |
1 files changed, 11 insertions, 16 deletions
diff --git a/ext/http/00_serve.js b/ext/http/00_serve.js index 422ce2fe3..47bbb7e7b 100644 --- a/ext/http/00_serve.js +++ b/ext/http/00_serve.js @@ -680,6 +680,15 @@ function serveHttpOn(context, callback) { let currentPromise = null; const promiseIdSymbol = SymbolFor("Deno.core.internalPromiseId"); + const promiseErrorHandler = (error) => { + // Abnormal exit + console.error( + "Terminating Deno.serve loop due to unexpected error", + error, + ); + context.close(); + }; + // Run the server const finished = (async () => { const rid = context.serverRid; @@ -689,14 +698,7 @@ function serveHttpOn(context, callback) { // Attempt to pull as many requests out of the queue as possible before awaiting. This API is // a synchronous, non-blocking API that returns u32::MAX if anything goes wrong. while ((req = op_http_try_wait(rid)) !== 0xffffffff) { - PromisePrototypeCatch(callback(req), (error) => { - // Abnormal exit - console.error( - "Terminating Deno.serve loop due to unexpected error", - error, - ); - context.close(); - }); + PromisePrototypeCatch(callback(req), promiseErrorHandler); } currentPromise = op_http_wait(rid); if (!ref) { @@ -713,14 +715,7 @@ function serveHttpOn(context, callback) { if (req === 0xffffffff) { break; } - PromisePrototypeCatch(callback(req), (error) => { - // Abnormal exit - console.error( - "Terminating Deno.serve loop due to unexpected error", - error, - ); - context.close(); - }); + PromisePrototypeCatch(callback(req), promiseErrorHandler); } for (const streamRid of new SafeSetIterator(context.responseBodies)) { |