@@ -548,14 +548,16 @@ would provide the exports interface for the instantiation of `module.wasm`.
548548
549549## Top-level ` await `
550550
551+ <!--
552+ added: v14.8.0
553+ -->
554+
551555> Stability: 1 - Experimental
552556
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.
555558
556559Assuming an ` a .mjs ` with
557560
558- <!-- eslint-skip -->
559561` ` ` js
560562export const five = await Promise .resolve (5 );
561563` ` `
@@ -572,6 +574,23 @@ console.log(five); // Logs `5`
572574node b .mjs # works
573575` ` `
574576
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+
575594<i id="esm_experimental_loaders"></i>
576595
577596## Loaders
@@ -1344,7 +1363,6 @@ success!
13441363[Conditional exports ]: packages .md #packages_conditional_exports
13451364[Core modules]: modules .md #modules_core_modules
13461365[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/
13481366[ES Module Integration Proposal for Web Assembly]: https: // github.com/webassembly/esm-integration
13491367[Node .js Module Resolution Algorithm]: #esm_resolver_algorithm_specification
13501368[Terminology]: #esm_terminology
@@ -1372,6 +1390,7 @@ success!
13721390[cjs- module - lexer]: https: // github.com/guybedford/cjs-module-lexer/tree/1.2.2
13731391[custom https loader]: #esm_https_loader
13741392[special scheme]: https: // url.spec.whatwg.org/#special-scheme
1393+ [status code]: process .md #process_exit_codes
13751394[the official standard format]: https: // tc39.github.io/ecma262/#sec-modules
13761395[transpiler loader example]: #esm_transpiler_loader
13771396[url .pathToFileURL ]: url .md #url_url_pathtofileurl_path
0 commit comments