Hacker News new | ask | show | jobs
by bebrws 1233 days ago
There are a ton of other approaches that would work with NodeJS that would be even more simple though right? The easiest or first that comes to mind is just have your function that does whatever functionality is required for creating this assetsManifest (whatever both compileServer and compileBrowser depend on). Lets call this createAssetsManifest. Then you have your 2 compile async functions but you just don't await on them and let them run without using the await keyword.

        const manifest = await createAssetsManifest();
        compileBrowser(manifest);
        compileServer(manifest);

I am wondering if since the creation of async/await that the idea behind callbacks and async operations in general in Node has been forgotten.

Not referring to the author or anyone in particular. Just a thought since these keywords might used so often by engineers new to Node that they might not have ever learned what came before?

2 comments

Even better, make use of the built-in promise helpers:

  const manifest = await createAssetsManifest();
  await Promise.all([
    compileBrowser(manifest),
    compileServer(manifest)
  ]);
That makes for such nice, clear code!
The manifest is an artifact produced by the browser compilation, but it can be shared _before_ the browser compilation writes its results to disk.

I _could_ refactor to have "browser compilation phase 1", then assets manifest, and then "browser compilation phase 2", but that's not how I model it in my head. Plus it would mean a diverging interface for `compileBrowser` and `compileServer` which doesn't fit my mental model either.

So prefer to use channels instead.

> but that's not how I model it in my head.

I think this is the central matter when it comes to primitives for asynchronous programming.

There exist many ways we can think about async tasks. The JavaScript ecosystem provides for multiple. i.e., event callbacks, async/await, generators, and more.

Programmer reach for tools which best match how we have learned to model these problems in our heads.

It's okay for people to use what works best for them.