3 ms·
I'm still not a fan of the async iterator API example for server handlers: for await (const conn of Deno.listen({ port: 4500 })) { (async () => {
by lxe 5y ago
I'm still not a fan of the async iterator API example for server handlers:
for await (const conn of Deno.listen({ port: 4500 })) {
(async () => {
for await (const { respondWith } of Deno.serveHttp(conn)) {
respondWith(new Response("Hello World"));
}
})();
}
It's hard to reason, and ugly with that async iffe in the middle adding layers of nesting and bf-like `})();` junk.
I think the event subscription API is much easier on the eyes and the mind.
- emptysea 5y agoI'm curious why the IIFE is even necessary in this case. There is already a top level await in the `for await` right? Looks fine IMHO once the IFFE is removed: for await (const conn of Deno.listen({ port: 4500 })) { for await (const { respondWith } of Deno.serveHttp(conn)) { respondWith(new Response("Hello World")); } } Edit: Oh is it so it runs in concurrently and avoids the synchronous handling?
- maddyboo 5y agoYes I think the IIFE is there to avoid the inner loop blocking the outer loop. I think it looks better with the IIFE factored out into its own function: async function serve(conn) { for await (const { respondWith } of Deno.serveHttp(conn)) { respondWith(new Response("Hello World")); } } for await (const conn of Deno.listen({ port: 4500 })) { serve(conn); }