@@ -63,6 +63,8 @@ let debug = require('internal/util/debuglog').debuglog('esm', (fn) => {
6363 debug = fn ;
6464} ) ;
6565
66+ const { isPromise } = require ( 'internal/util/types' ) ;
67+
6668/**
6769 * @typedef {import('./hooks.js').HooksProxy } HooksProxy
6870 * @typedef {import('./module_job.js').ModuleJobBase } ModuleJobBase
@@ -582,15 +584,21 @@ class ModuleLoader {
582584
583585 /**
584586 * Load a module and translate it into a ModuleWrap for ordinary imported ESM.
585- * This is run asynchronously.
587+ * This may be run asynchronously if there are asynchronous module loader hooks registered .
586588 * @param {string } url URL of the module to be translated.
587589 * @param {object } loadContext See {@link load}
588590 * @param {boolean } isMain Whether the module to be translated is the entry point.
589- * @returns {Promise<ModuleWrap> }
591+ * @returns {Promise<ModuleWrap>|ModuleWrap }
590592 */
591- async loadAndTranslate ( url , loadContext , isMain ) {
592- const { format, source } = await this . load ( url , loadContext ) ;
593- return this . #translate( url , format , source , isMain ) ;
593+ loadAndTranslate ( url , loadContext , isMain ) {
594+ const maybePromise = this . load ( url , loadContext ) ;
595+ const afterLoad = ( { format, source } ) => {
596+ return this . #translate( url , format , source , isMain ) ;
597+ } ;
598+ if ( isPromise ( maybePromise ) ) {
599+ return maybePromise . then ( afterLoad ) ;
600+ }
601+ return afterLoad ( maybePromise ) ;
594602 }
595603
596604 /**
0 commit comments