@@ -585,15 +585,16 @@ would provide the exports interface for the instantiation of `module.wasm`.
585
585
586
586
## Top-level ` await `
587
587
588
+ <!--
589
+ added: v14.8.0
590
+ -->
591
+
588
592
> Stability: 1 - Experimental
589
593
590
- The ` await ` keyword may be used in the top level (outside of async functions)
591
- within modules as per the [ECMAScript Top-Level ` await ` proposal][].
594
+ The ` await ` keyword may be used in the top level body of an ECMAScript module.
592
595
593
596
Assuming an ` a .mjs ` with
594
597
595
- <!-- eslint-skip -->
596
-
597
598
` ` ` js
598
599
export const five = await Promise .resolve (5 );
599
600
` ` `
@@ -610,6 +611,23 @@ console.log(five); // Logs `5`
610
611
node b .mjs # works
611
612
` ` `
612
613
614
+ If a top level ` await ` expression never resolves, the ` node` process will exit
615
+ with a ` 13 ` [status code][].
616
+
617
+ ` ` ` js
618
+ import { spawn } from ' child_process' ;
619
+ import { execPath } from ' process' ;
620
+
621
+ spawn (execPath, [
622
+ ' --input-type=module' ,
623
+ ' --eval' ,
624
+ // Never-resolving Promise:
625
+ ' await new Promise(() => {})' ,
626
+ ]).once (' exit' , (code ) => {
627
+ console .log (code); // Logs `13`
628
+ });
629
+ ` ` `
630
+
613
631
<i id="esm_experimental_loaders"></i>
614
632
615
633
## Loaders
@@ -1441,7 +1459,6 @@ success!
1441
1459
[Conditional exports ]: packages .md #conditional- exports
1442
1460
[Core modules]: modules .md #core- modules
1443
1461
[Dynamic ` import()` ]: https: // wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Dynamic_Imports
1444
- [ECMAScript Top- Level ` await` proposal]: https: // github.com/tc39/proposal-top-level-await/
1445
1462
[ES Module Integration Proposal for WebAssembly]: https: // github.com/webassembly/esm-integration
1446
1463
[Import Assertions]: #import -assertions
1447
1464
[Import Assertions proposal]: https: // github.com/tc39/proposal-import-assertions
@@ -1476,5 +1493,6 @@ success!
1476
1493
[percent- encoded]: url .md #percent- encoding- in - urls
1477
1494
[resolve hook]: #resolvespecifier- context- defaultresolve
1478
1495
[special scheme]: https: // url.spec.whatwg.org/#special-scheme
1496
+ [status code]: process .md #exit- codes
1479
1497
[the official standard format]: https: // tc39.github.io/ecma262/#sec-modules
1480
1498
[url .pathToFileURL ]: url .md #urlpathtofileurlpath
0 commit comments