News for hackers. Stuff that matters.
Re:Node.js 16 Available Now by dfabulich on Tuesday April 20, 2021 @02:13PM (#26878550)
The new stable timers API in Node 16, combined with top-level await, means that you can now easily sleep in an ESM Node script, like this:

    import { setTimeout } from 'timers/promises';

    await setTimeout(1000);
    console.log("awake");
(But note that you'll have to activate ESM mode to write this script, e.g. by writing it in a `.mjs` file instead of a `.js` file or by adding a setting to package.json.) https://redfin.engineering/node-modules-at-war-why-commonjs-...
Re:Node.js 16 Available Now by eloff on Tuesday April 20, 2021 @02:35PM (#26879021)
You can also do this with a wrapper around setTimeout, if you're in an older environment:

  /\*
   \* A sleep function that returns a promise.
   \*
   \* @example
   \* Sleep for 100ms
   \* ```
   \* await wait(100);
   \* ```
   \*/
  export async function wait(ms: number) {
      return new Promise(resolve => {
          setTimeout(resolve, ms);
      });
  }
Re:Node.js 16 Available Now by davidkunz on Tuesday April 20, 2021 @04:08PM (#26880822)
... or even more easily by:

const wait = require('util').promisify(setTimeout)

Re:Node.js 16 Available Now by Mogzol on Tuesday April 20, 2021 @04:24PM (#26881077)
That's interesting. util.promisify normally only works with functions where the callback is the final argument, which isn't the case with setTimeout. I didn't know you could customize promisify behaviour, but looking at the docs apparently you can just by setting a property on the object using the util.promisify.custom symbol [1]. That is what setTimeout is doing, which is why that code works.

[1] https://nodejs.org/dist/latest-v16.x/docs/api/util.html#util...

Re:Node.js 16 Available Now by motogpjimbo on Wednesday April 21, 2021 @03:34AM (#26886463)
You don't need `async` here, since your function explicitly returns a promise.
Re:Node.js 16 Available Now by coding123 on Wednesday April 21, 2021 @12:30PM (#26891824)
I do that too. In ts, it helps a tiny bit - it enforces that the function will return a promise. So if that ever changes, the function TS compiler will be your backup anchor.
Re:Node.js 16 Available Now by whatl3y on Wednesday April 21, 2021 @07:15AM (#26888016)
While this is true, I've found over time it's easier for me to quickly see in my code if I need to `await` on something if the function is an `async` function and not just that it returns a Promise.

I prefer

async function foo() { return await new Promise(...) }

as opposed to

function foo() { return new Promise(...) }

They're the same thing for the most part but the latter I have to potentially dig deeper into the function to confirm it returns a promise compared to the former.

Re:Node.js 16 Available Now by junon on Wednesday April 21, 2021 @09:03AM (#26889109)
The problem is that this is more than just a syntactic difference. There's a chance it will take two ticks to resolve instead of one.

For documentation purposes, I recommend block-commenting the `async`:

const foo = /async/ () => new Promise(...);

Re:Node.js 16 Available Now by whatl3y on Thursday April 22, 2021 @07:44AM (#26901629)
I did not know this and appreciate the insight! Will definitely store this back of mind and try and remember to use block comments when needing to explicitly return a promise.
Re:Node.js 16 Available Now by eloff on Wednesday April 21, 2021 @01:41PM (#26892931)
That's not true if you just return the promise instead of using return await, right?
Re:Node.js 16 Available Now by junon on Wednesday April 21, 2021 @10:04PM (#26897903)
No. `async` on a function automatically defers it even if the return value is immediate.

> async function foo() { return "hello"; } foo().then(console.log); console.log("after")

after hello

Re:Node.js 16 Available Now by junon on Wednesday April 21, 2021 @09:01AM (#26889080)
const sleep = ms => new Promise(r => setTimeout(r, ms));

It doesn't need to be that tricky.

Re:Node.js 16 Available Now by eloff on Wednesday April 21, 2021 @01:44PM (#26892983)
I get that the arrow function notation is less verbose, but I disagree with calling function syntax "tricky". It really doesn't matter much. You need the type on ms though (if using typescript), and export.
Re:Node.js 16 Available Now by mjgs on Tuesday April 20, 2021 @09:03PM (#26884169)
What’s the overall feeling on using .mjs files?

My initial thoughts were “urgh I just want to have .js files in my projects” but I’m wondering if I’ll warm to them given that they make things a lot easier.

Are people eventually just all going to use .mjs files for everything?

Re:Node.js 16 Available Now by shriek on Tuesday April 20, 2021 @11:52PM (#26885245)
If you have package.json file you can still use .js file with 'type: module' in package.json.

https://nodejs.org/api/esm.html#esm_enabling

Re:Node.js 16 Available Now by mjgs on Wednesday April 21, 2021 @02:01AM (#26885906)
Yeah but it seems like it might be easier to just start using .mjs everywhere, no?
Re:Node.js 16 Available Now by ranguna on Wednesday April 21, 2021 @02:32AM (#26886065)
I don't think so.
Re:Node.js 16 Available Now by mjgs on Wednesday April 21, 2021 @09:40AM (#26889537)
I know it’s a bit of an apples and pears comparison, but which part of changing the file extension feels more difficult than editing package.json?

I’m thinking about new projects rather than converting existing projects.

One benefit I can see to .mjs is that if all extensions are .mjs it’s clear what type of project it is without the need to open up package.json.

Re:Node.js 16 Available Now by ranguna on Thursday April 22, 2021 @02:42AM (#26899523)
With the context you've added now, I might agree. Existing projects would be a pain to rename all files compared to adding a single line to the package.json .

About new projects, I'd have to read more about this new extension, for some reason it feels like a temporary solution that'll just get merged to the normal js extension in a future release.

Re:Node.js 16 Available Now by mjgs on Thursday April 22, 2021 @05:44AM (#26900724)
Yeah it totally has the feel of a temporary solution.

From what I’ve read though, there doesn’t appear to be much of a plan apart from the current solution.

My initial reaction is that I want to keep all my files with .js extension.

One thing that could potentially be problematic is for files that need to run in the browser and on the server.

Really curious to see where the community’s tea leaves settle. My guess is that people will eventually just start using the new extension.

Re:Node.js 16 Available Now by mariushn on Thursday April 22, 2021 @03:21AM (#26899774)
Can I have an entire stand-alone app (not a module to be published on npm) as a module in package.json? I understand it can no longer use require() anywhere, that's ok.
Re:Node.js 16 Available Now by capableweb on Tuesday April 20, 2021 @02:34PM (#26879013)
The question is, why bloat the standard library with something that would take a couple of lines to implement yourself or as a library? More languages should release new features as libraries instead of forcing it into the "global" space of the standard library.
Re:Node.js 16 Available Now by geewee on Tuesday April 20, 2021 @02:36PM (#26879038)
I think this is the only "Javascript has too few third party libraries" take I've ever heard.
Re:Node.js 16 Available Now by capableweb on Tuesday April 20, 2021 @02:46PM (#26879276)
Never said it should be a third party library, the core team can release libraries as well if they wanted to you know.
Re:Node.js 16 Available Now by hn_throwaway_99 on Tuesday April 20, 2021 @02:55PM (#26879500)
I think what you are arguing (which I agree with) is that the Node ecosystem would benefit from a set of "core" libraries that could just be installed as a separate NPM module, that are blessed by the Node core maintainers, but aren't part of a specific Node version.

Deno basically does this with the Deno standard library, e.g. https://deno.land/[email protected], and I agree that I think it's the right approach.

There is nothing "special" about wrapping setTimeout as a Promise, indeed pretty much everyone has done it at some point, so would be nice if there was a single, blessed standard version that I could just add as @node/standard in my package.json as long as I was on any supported Node version.

Re:Node.js 16 Available Now by throwanem on Tuesday April 20, 2021 @03:33PM (#26880207)
The core team does release a library. It comes with every install, and that's why we call it "standard".
Re:Node.js 16 Available Now by capableweb on Tuesday April 20, 2021 @03:45PM (#26880423)
The original point was that not everyone want all these syntactic sugar "features" shipped with the core runtime...
Re:Node.js 16 Available Now by jakelazaroff on Tuesday April 20, 2021 @03:53PM (#26880552)
What’s the syntactic sugar feature here? Top-level await is part of the language spec.
Re:Node.js 16 Available Now by throwanem on Tuesday April 20, 2021 @04:15PM (#26880945)
One into which you have to opt in, yet.
Re:Node.js 16 Available Now by eyelidlessness on Tuesday April 20, 2021 @04:45PM (#26881400)
I don’t think that’s true. It’s supported in any ESM module, per the ESM standard, without any config or runtime flags. If you mean using ESM to be the opt-in mechanism, that’s not a meaningful distinction. CJS modules and require have always been synchronous and likely always will be. Changing that would break too many things.
Re:Node.js 16 Available Now by fny on Tuesday April 20, 2021 @02:37PM (#26879060)
Because I don't want to have to download some yahoos library just to have a timeout that behaves like a promise.

I prefer my batteries included. Also importing from "timers/promises" isn't a global namespace.

Re:Node.js 16 Available Now by cipher_system on Tuesday April 20, 2021 @02:48PM (#26879316)
I can never remember if i'm using promise-sleep, sleep-promise or any of the other available packages. Or if they stop working on a node upgrade. Also nice if people use and do the same simple things across projects.

The most common stuff I like to be part of the standard lib, either node or javascript, but it works fairly well now I guess so no biggie.

Re:Node.js 16 Available Now by megous on Tuesday April 20, 2021 @04:16PM (#26880972)
This one is not that bad. At least it takes AbortSignal, so it can be cancelled. Bonus points for that.

That said, I'll probably spend more time searching the node docs for how to import/use it than just implementing it myself.

OTOH I use promisified timer in almost all of my web scraping scripts, to reduce the load on the server, so I'm glad I'll be able to drop this thing from my utility library.

Re:Node.js 16 Available Now by RedShift1 on Tuesday April 20, 2021 @03:12PM (#26879850)
That kind of reasoning got us into the whole pad-left misery.
Re:Node.js 16 Available Now by capableweb on Tuesday April 20, 2021 @03:46PM (#26880441)
No, mutable package management is the main reason, together with "humanness" as the author pulled the package from the registry. The core team would hardly pull a package like that.
Re:Node.js 16 Available Now by ReptileMan on Tuesday April 20, 2021 @03:22PM (#26880027)
Like left pad
Re:Node.js 16 Available Now by w-j-w on Tuesday April 20, 2021 @02:44PM (#26879226)
Large standard libraries are good. The more things are in the standard library for a language, the more likely it is that other 3rd party libraries play nice together.