@@ -548,14 +548,16 @@ would provide the exports interface for the instantiation of `module.wasm`.
548
548
549
549
## Top-level ` await `
550
550
551
+ <!--
552
+ added: v14.8.0
553
+ -->
554
+
551
555
> Stability: 1 - Experimental
552
556
553
- The ` await ` keyword may be used in the top level (outside of async functions)
554
- within modules as per the [ECMAScript Top-Level ` await ` proposal][].
557
+ The ` await ` keyword may be used in the top level body of an ECMAScript module.
555
558
556
559
Assuming an ` a .mjs ` with
557
560
558
- <!-- eslint-skip -->
559
561
` ` ` js
560
562
export const five = await Promise .resolve (5 );
561
563
` ` `
@@ -572,6 +574,23 @@ console.log(five); // Logs `5`
572
574
node b .mjs # works
573
575
` ` `
574
576
577
+ If a top level ` await ` expression never resolves, the ` node` process will exit
578
+ with a ` 13 ` [status code][].
579
+
580
+ ` ` ` js
581
+ import { spawn } from ' child_process' ;
582
+ import { execPath } from ' process' ;
583
+
584
+ spawn (execPath, [
585
+ ' --input-type=module' ,
586
+ ' --eval' ,
587
+ // Never-resolving Promise:
588
+ ' await new Promise(() => {})' ,
589
+ ]).once (' exit' , (code ) => {
590
+ console .log (code); // Logs `13`
591
+ });
592
+ ` ` `
593
+
575
594
<i id="esm_experimental_loaders"></i>
576
595
577
596
## Loaders
@@ -1344,7 +1363,6 @@ success!
1344
1363
[Conditional exports ]: packages .md #packages_conditional_exports
1345
1364
[Core modules]: modules .md #modules_core_modules
1346
1365
[Dynamic ` import()` ]: https: // wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Dynamic_Imports
1347
- [ECMAScript Top- Level ` await` proposal]: https: // github.com/tc39/proposal-top-level-await/
1348
1366
[ES Module Integration Proposal for Web Assembly]: https: // github.com/webassembly/esm-integration
1349
1367
[Node .js Module Resolution Algorithm]: #esm_resolver_algorithm_specification
1350
1368
[Terminology]: #esm_terminology
@@ -1372,6 +1390,7 @@ success!
1372
1390
[cjs- module - lexer]: https: // github.com/guybedford/cjs-module-lexer/tree/1.2.2
1373
1391
[custom https loader]: #esm_https_loader
1374
1392
[special scheme]: https: // url.spec.whatwg.org/#special-scheme
1393
+ [status code]: process .md #process_exit_codes
1375
1394
[the official standard format]: https: // tc39.github.io/ecma262/#sec-modules
1376
1395
[transpiler loader example]: #esm_transpiler_loader
1377
1396
[url .pathToFileURL ]: url .md #url_url_pathtofileurl_path
0 commit comments