有没有办法在 es 模块中导出承诺的结果?



由于await只能在异步函数中使用,因此下面的代码不起作用。

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

那么有没有办法在 es/ts 模块中导出承诺的结果呢?

deno可以支持top-level await

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

v8/chrome的这个功能正在开发中:https://v8.dev/features/top-level-await。

来自文档,

"await 运算符用于等待承诺。它只能在异步函数中使用。

如果打算在全局范围内使用异步等待,请参阅此问题以获取解决方法。

最新更新