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