@@ -255,6 +255,7 @@ const kNoError = Symbol('kNoError');
255
255
class SourceTextModule extends Module {
256
256
#error = kNoError ;
257
257
#statusOverride;
258
+ #waitingLinkPromises;
258
259
259
260
constructor ( sourceText , options = { } ) {
260
261
validateString ( sourceText , 'sourceText' ) ;
@@ -308,31 +309,37 @@ class SourceTextModule extends Module {
308
309
this [ kLink ] = async ( linker ) => {
309
310
this . #statusOverride = 'linking' ;
310
311
311
- const promises = this [ kWrap ] . link ( async ( identifier ) => {
312
- const module = await linker ( identifier , this ) ;
313
- if ( module [ kWrap ] === undefined ) {
314
- throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
315
- }
316
- if ( module . context !== this . context ) {
317
- throw new ERR_VM_MODULE_DIFFERENT_CONTEXT ( ) ;
318
- }
319
- if ( module . status === 'errored' ) {
320
- throw new ERR_VM_MODULE_LINKING_ERRORED ( ) ;
321
- }
322
- if ( module . status === 'unlinked' ) {
323
- await module [ kLink ] ( linker ) ;
324
- }
325
- return module [ kWrap ] ;
326
- } ) ;
327
-
328
312
try {
313
+ const promises = this [ kWrap ] . link ( async ( identifier ) => {
314
+ const module = await linker ( identifier , this ) ;
315
+ if ( module [ kWrap ] === undefined ) {
316
+ throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
317
+ }
318
+ if ( module . context !== this . context ) {
319
+ throw new ERR_VM_MODULE_DIFFERENT_CONTEXT ( ) ;
320
+ }
321
+ if ( module . status === 'errored' ) {
322
+ throw new ERR_VM_MODULE_LINKING_ERRORED ( ) ;
323
+ }
324
+ if ( module . status === 'unlinked' ) {
325
+ await module [ kLink ] ( linker ) ;
326
+ }
327
+ if ( module . status === 'linking' ) {
328
+ await module . #waitingLinkPromises;
329
+ }
330
+ return module [ kWrap ] ;
331
+ } ) ;
332
+
329
333
if ( promises !== undefined ) {
330
- await PromiseAll ( promises ) ;
334
+ assert ( this . #waitingLinkPromises === undefined ) ;
335
+ this . #waitingLinkPromises = PromiseAll ( promises ) ;
331
336
}
337
+ await this . #waitingLinkPromises;
332
338
} catch ( e ) {
333
339
this . #error = e ;
334
340
throw e ;
335
341
} finally {
342
+ this . #waitingLinkPromises = undefined ;
336
343
this . #statusOverride = undefined ;
337
344
}
338
345
} ;
0 commit comments