@@ -591,15 +591,16 @@ would provide the exports interface for the instantiation of `module.wasm`.
591591
592592## Top-level ` await `
593593
594+ <!--
595+ added: v14.8.0
596+ -->
597+
594598> Stability: 1 - Experimental
595599
596- The ` await ` keyword may be used in the top level (outside of async functions)
597- within modules as per the [ECMAScript Top-Level ` await ` proposal][].
600+ The ` await ` keyword may be used in the top level body of an ECMAScript module.
598601
599602Assuming an ` a .mjs ` with
600603
601- <!-- eslint-skip -->
602-
603604` ` ` js
604605export const five = await Promise .resolve (5 );
605606` ` `
@@ -616,6 +617,23 @@ console.log(five); // Logs `5`
616617node b .mjs # works
617618` ` `
618619
620+ If a top level ` await ` expression never resolves, the ` node` process will exit
621+ with a ` 13 ` [status code][].
622+
623+ ` ` ` js
624+ import { spawn } from ' child_process' ;
625+ import { execPath } from ' process' ;
626+
627+ spawn (execPath, [
628+ ' --input-type=module' ,
629+ ' --eval' ,
630+ // Never-resolving Promise:
631+ ' await new Promise(() => {})' ,
632+ ]).once (' exit' , (code ) => {
633+ console .log (code); // Logs `13`
634+ });
635+ ` ` `
636+
619637<i id="esm_experimental_loaders"></i>
620638
621639## Loaders
@@ -1426,7 +1444,6 @@ success!
14261444[Conditional exports]: packages.md#conditional-exports
14271445[Core modules]: modules.md#core-modules
14281446[Dynamic `import()`]: https:// wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Dynamic_Imports
1429- [ECMAScript Top-Level `await` proposal]: https:// github.com/tc39/proposal-top-level-await/
14301447[ES Module Integration Proposal for WebAssembly]: https:// github.com/webassembly/esm-integration
14311448[Import Assertions]: #import-assertions
14321449[Import Assertions proposal]: https:// github.com/tc39/proposal-import-assertions
@@ -1461,5 +1478,6 @@ success!
14611478[percent-encoded]: url.md#percent-encoding-in-urls
14621479[resolve hook]: #resolvespecifier-context-defaultresolve
14631480[special scheme]: https:// url.spec.whatwg.org/#special-scheme
1481+ [status code]: process.md#exit-codes
14641482[the official standard format]: https:// tc39.github.io/ecma262/#sec-modules
14651483[url.pathToFileURL]: url.md#urlpathtofileurlpath
0 commit comments