Hacker News new | ask | show | jobs
by spankalee 1233 days ago
This is a case where a function is conceptually producing two promises: one for an intermediate result and one for its final result.

I would attack this one of two ways, both of which I feel are more idiomatic than trying to emulate Go in JS:

1) Factor out the intermediate computation and promise:

    const manifestPromise = buildManifest();
    await Promise.all([
      compileBrowser({manifestPromise});
      compileServer({manifestPromise});
    ]);
2) Just return the two promises from compilerBrowser():

    function compilerBrowser() {
      let resolveManifest;
      const manifest = new Promise((res) => resolveManifest = res);

      const result = (async () => {
        // Compute manifest and resolve it before the final result:
        resolveManifest(manifest);

        // Compute the result of the result...
        return finalResult;
      }());

      return {
        manifest,
        result;
      };
    }
IOW, decompose things into smaller pieces (1) and/or compose them into values that match what you need (2) and remember that a function can return a group of promises instead of a single one.