HNHacker News
TopNewBestAskShowJobs

patdx

4 karma · joined February 24, 2021

submissionscomments
patdx··on Node.js 20 is now available
The big problem is Express code like the following will either hang the HTTP request forever (--unhandled-rejections=warn), until the client times out and gives up, or crash your whole server, taking down any other HTTP requests in progress (--unhandled-rejections=throw):

  const db = {
    async getUsers() {
      throw new Error("failed to connect to database");
    },
  };

  app.get("/", async (req, res) => {
    const result = await db.getUsers();
    return result;
  });
There are various monkey patches/wrappers you can use to make async errors work the same as sync errors, but it is easy to forget and hard to understand, especially for newbies. Many other frameworks handle async/sync errors in a more consistent way.
patdx··on React is holding me hostage
That’s a good point, that example does feel a bit contrived. Interestingly though it is actually “canon” to call setState within the render function (for certain rare situations). I didn’t actually know this until I read the React beta docs: https://beta.reactjs.org/learn/you-might-not-need-an-effect#...
patdx··on The Pretty JSON Revolution
Basically yes, though `{` and `}` also start and end expression blocks in JavaScript. So the JS formatter needs to be aware that it is working with a JSON object and not a complete expression.

Valid JSON:

  {
    "key1": "hello",
    "key2": "world"
  }
You could "trick" a JS formatter to format it by wrapping with a fake function, etc. Some minimum valid JS:

  json({
    key1: "hello",
    key2: "world",
  });
MongoDB has some JS libraries that use similar tricks to use JS parsers for their shell query format (which is similar to JSON). For example, around line 597: https://unpkg.com/browse/ejson-shell-parser@1.1.1/dist/ejson...