Saltar al contenido ForoHackers

Node 23 let require() load my module. Then I put a kettle on inside the module.

JavaScript y TypeScriptCódigo abiertoToolchains y enlazadores
#16373 · · Citar · Denunciar

Node 23.0.0 is out. In my tiny local CommonJS program, require('./labels.mjs') gives me the exports from an ES module that just defines a label. I put a top-level await into a second copy and the exact same caller wants to go home. Does enabling experimental require(esm) by default entitle me to a synchronous cup of tea or did I misread the menu?

#16579 · · Citar · Denunciar

For the record the synchronous version is export const label = 'hinge'. The second version has await Promise.resolve() before the export. I'm not waiting for the network. I can't even blame the kettle.

#16580 · · Citar · Denunciar

The timing of your promise doesn't change the module's shape. The graph with top-level await is asynchronous. A synchronous require caller needs to get its result before it can continue. What does the error code say?

#16581 · · Citar · Denunciar

ERR_REQUIRE_ASYNC_MODULE. The boring file works, the one with the immediately resolved promise doesn't. I expected it to inspect the promise, decide it was negligible, and otherwise run my life for me.

#16582 · · Citar · Denunciar

Si la aplicación admite una operación asíncrona ahí, import('./labels.mjs') devuelve una promesa; espera el resultado en un lugar que pueda esperar. Si tu función pública promete devolver la etiqueta al instante, cambiarla en silencio por una promesa va a fastidiar a todos los que la usan.

#16583 · · Citar · Denunciar

We once published a benchmark for "waiting zero seconds". I am delighted you have found the one kettle that makes our results look hasty.

#16584 · · Citar · Denunciar

I moved the asynchronous choice up to the outer function and kept the instant little label file instant. Parent awaits the dynamic import of the async one. It's not my favourite answer, which is usually a sign that I've learnt something.

#16585 · · Citar · Denunciar

And leave the actual version in the bug note. This is Node 23.0.0's experimental behaviour. Here it is in their words: https://nodejs.org/en/blog/release/v23.0.0

No has iniciado sesión, así que esto se publica como anónimo: no se guarda con ello ninguna cuenta, nombre ni dirección, y no puede llevar enlaces ni imágenes. Inicia sesión o únete para publicar con tu nombre. Los registros de nuestro servidor web guardan la dirección y la hora de cada petición hasta 14 días; para que la tuya no quede en ellos, usa Tor. Para escribir al equipo de forma confidencial, usa el buzón. Firmar como fuente habitual (una clave guardada solo en este navegador)

Enviar tarda unos segundos aquí: tu navegador resuelve antes una prueba de trabajo contra el spam.

Responder