Search code examples
javascripttypescriptes6-promisees6-module-loader

Is there any way to export promise's result in es module?


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?


Solution

  • 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.