@@ -590,15 +590,16 @@ would provide the exports interface for the instantiation of `module.wasm`.
590
590
591
591
## Top-level ` await `
592
592
593
+ <!--
594
+ added: v14.8.0
595
+ -->
596
+
593
597
> Stability: 1 - Experimental
594
598
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.
597
600
598
601
Assuming an ` a .mjs ` with
599
602
600
- <!-- eslint-skip -->
601
-
602
603
` ` ` js
603
604
export const five = await Promise .resolve (5 );
604
605
` ` `
@@ -615,6 +616,23 @@ console.log(five); // Logs `5`
615
616
node b .mjs # works
616
617
` ` `
617
618
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
+
618
636
<i id="esm_experimental_loaders"></i>
619
637
620
638
## Loaders
@@ -1425,7 +1443,6 @@ success!
1425
1443
[Conditional exports]: packages.md#conditional-exports
1426
1444
[Core modules]: modules.md#core-modules
1427
1445
[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/
1429
1446
[ES Module Integration Proposal for WebAssembly]: https:// github.com/webassembly/esm-integration
1430
1447
[Import Assertions]: #import-assertions
1431
1448
[Import Assertions proposal]: https:// github.com/tc39/proposal-import-assertions
@@ -1460,5 +1477,6 @@ success!
1460
1477
[percent-encoded]: url.md#percent-encoding-in-urls
1461
1478
[resolve hook]: #resolvespecifier-context-defaultresolve
1462
1479
[special scheme]: https:// url.spec.whatwg.org/#special-scheme
1480
+ [status code]: process.md#exit-codes
1463
1481
[the official standard format]: https:// tc39.github.io/ecma262/#sec-modules
1464
1482
[url.pathToFileURL]: url.md#urlpathtofileurlpath
0 commit comments