1

As await can only be used inside an async function, the code below does not work.

const config_path = 'https://test.cdn.com/conf.' + location.hash.include('DEV')?'development.ts':'production.ts'
const promise = import(config_path)

export default config = await promise

So is there any way to export promise's result in es/ts module?

ahuigo
  • 2,929
  • 2
  • 25
  • 45

2 Answers2

1

The deno can support top-level await:

 await Promise.resolve(console.log(''));
 const strings = await import(`/i18n/${navigator.language}`);

And this feature for v8/chrome is in developing: https://v8.dev/features/top-level-await.

ahuigo
  • 2,929
  • 2
  • 25
  • 45
-1

from the docs,

"The await operator is used to wait for a Promise. It can only be used inside an async function."

If you intend to use async await on global scope, see this question for workarounds.

laxman
  • 1,781
  • 4
  • 14
  • 32