#2473·systemjs

Top-level await does not wait for its dependencies

Author: qingwaboteCreated Nov 26, 2023Updated Sep 2, 2024
Labelsbug

Demonstration

a.ts

typescript
let a:string;

async function delay():Promise<void> {
    return new Promise(function(resolve){
        setTimeout(() => {
            resolve();
        }, 1000);
    })
}

await delay();

a = 'a';

export {a};

b.ts

typescript
import {a} from './a.js'

console.log('a in b',a)

c.ts

typescript
import {a} from './a.js'

console.log('a in c',a)

index.ts

typescript
export * from './a.js'
export * from './b.js'
export * from './c.js'

index.html

xml
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
</head>

<body>
  <script src="./dist/s.js"></script>
  <script type="systemjs-module" src="./dist/index.js"></script>
</body>

</html>

tsconfig.json

json
{
  "compilerOptions": {
    "target": "ES2017", 
    "module": "System", 
    "moduleResolution": "node", 
    "allowJs": true, 
    "sourceMap": false, 
    "outDir": "dist", 
    "esModuleInterop": true, 
    "forceConsistentCasingInFileNames": true, 
    "strict": true,
    "skipLibCheck": true 
  }
}

tsc them to systemjs module and run web server

Expected Behavior

two logs without error after 1000ms

Actual Behavior

b.js:12 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'a') at Object.execute (b.js:12:37) at doExec (s.js:429:40) at postOrderExec (s.js:426:16) at s.js:415:38 at Array.forEach () at postOrderExec (s.js:413:16) at s.js:381:20