systemjs / systemjs

Dynamic ES module loader

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Top-level await does not wait for its dependencies

qingwabote opened this issue · comments

Demonstration

a.ts

let a:string;

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

await delay();

a = 'a';

export {a};

b.ts

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

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

c.ts

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

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

index.ts

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

index.html

<!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

{
  "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